App Dev Worksheet: Functions
Developer's tip (Write once, use often): function name(parameter) { …; return result; } — call it with name(value). Work out each call separately, then finish the line. Inside a function, if / return can give different results for different inputs.
NameDateTime takenScore ___ / 40
- 1.🧰 How do you run (call) a function called greet? (a) run greet (b) greet[] (c) greet() (d) #greet
-
2.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 20;
}
show(fee(1) + fee(4)); -
3.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Meera")); (a) Meera (b) greet (c) Welcome, Meera! (d) Welcome, name! - 4.🧰 What does return do in a function? (a) deletes the app (b) closes the screen (c) repeats the code (d) sends back a value
- 5.🧰 Why write a function? (a) to reuse code (b) to make code longer (c) to hide errors (d) to slow the app
-
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(1)); - 7.🧰 In function tip(bill), what is bill? (a) a screen (b) a parameter (c) a loop (d) an event
-
8.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(16)); -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 4)); -
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 3)); - 11.🧰 What is a function? (a) a type of phone (b) a named block of code (c) a screen size (d) a list of colours
-
12.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 3)); -
13.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 5)); -
14.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 4)); -
15.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 4)); -
16.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Hi, Riya! (b) Riya (c) greet (d) Hi, name! -
17.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(21)); -
18.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(17)); -
19.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 30;
}
show(fee(8) + fee(3)); -
20.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(0)); -
21.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 4)); -
22.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 40;
}
show(fee(10) + fee(1)); -
23.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 40;
}
show(fee(5) + fee(11)); -
24.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(4)); -
25.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Hello, name! (b) Riya (c) greet (d) Hello, Riya! -
26.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 40;
}
show(fee(6) + fee(4)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 2)); -
28.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 30;
}
show(fee(2) + fee(8)); -
29.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Kabir")); (a) Well done, name! (b) greet (c) Well done, Kabir! (d) Kabir -
30.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 60;
}
show(fee(11) + fee(6)); -
31.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 5)); -
32.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(5)); -
33.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Riya")); (a) Well done, Riya! (b) Well done, name! (c) Riya (d) greet -
34.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(11)); -
35.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(5)); -
36.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 30;
}
show(fee(11) + fee(4)); -
37.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(26)); -
38.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); -
39.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(4)); -
40.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Arjun")); (a) Hi, Arjun! (b) Hi, name! (c) Arjun (d) greet
Answer key
- greet()
- 0
- Welcome, Meera!
- sends back a value
- to reuse code
- 40
- a parameter
- 32
- 40
- 45
- a named block of code
- 135
- 325
- 380
- 340
- Hi, Riya!
- 42
- 34
- 30
- 20
- 280
- 40
- 80
- 12
- Hello, Riya!
- 40
- 130
- 30
- Well done, Kabir!
- 120
- 450
- 80
- Well done, Riya!
- 33
- 100
- 30
- 78
- 60
- 80
- Hi, Arjun!
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students