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