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