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