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