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