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