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