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