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