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.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(32)); -
2.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(16)); -
3.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) greet (b) Aman (c) Hi, name! (d) Hi, Aman! - 4.🧰 How do you run (call) a function called greet? (a) #greet (b) greet() (c) run greet (d) greet[]
- 5.🧰 In function tip(bill), what is bill? (a) a screen (b) an event (c) a loop (d) a parameter
- 6.🧰 What does return do in a function? (a) sends back a value (b) closes the screen (c) repeats the code (d) deletes the app
-
7.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 50;
}
show(fee(4) + fee(8)); -
8.🧰 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(8) + fee(10)); -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 2)); -
10.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
11.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(32)); - 12.🧰 Why write a function? (a) to hide errors (b) to make code longer (c) to reuse code (d) to slow the app
-
13.🧰 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(3) + fee(6)); - 14.🧰 What is a function? (a) a named block of code (b) a list of colours (c) a screen size (d) a type of phone
-
15.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(1)); -
16.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 4)); -
17.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(4)); -
18.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Aman")); (a) Well done, Aman! (b) Aman (c) greet (d) Well done, name! -
19.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 5)); -
20.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
21.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) Hi, Kabir! (b) Hi, name! (c) Kabir (d) greet -
22.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Meera")); (a) Meera (b) greet (c) Well done, Meera! (d) Well done, name! -
23.🧰 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(1) + fee(6)); -
24.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(23)); -
25.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Kabir")); (a) Hello, Kabir! (b) Kabir (c) Hello, name! (d) greet -
26.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 2)); -
27.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(17)); -
28.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(2)); -
29.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(4)); -
30.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Riya (b) Hello, name! (c) greet (d) Hello, Riya! -
31.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(11)); -
32.🧰 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(4) + fee(3)); -
33.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(14)); -
34.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(5)); -
35.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 3)); -
36.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(5)); -
37.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(5)); -
38.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(0)); -
39.🧰 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(6) + fee(2)); -
40.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Hi, Riya! (b) Riya (c) greet (d) Hi, name!
Answer key
- 64
- 48
- Hi, Aman!
- greet()
- a parameter
- sends back a value
- 50
- 40
- 50
- 30
- 96
- to reuse code
- 40
- a named block of code
- 50
- 220
- 80
- Well done, Aman!
- 200
- 70
- Hi, Kabir!
- Well done, Meera!
- 30
- 46
- Hello, Kabir!
- 160
- 34
- 60
- 70
- Hello, Riya!
- 33
- 0
- 42
- 100
- 195
- 90
- 70
- 50
- 30
- Hi, Riya!
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students