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