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