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