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