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