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