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