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