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