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