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