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