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