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