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