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