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