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