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