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