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