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