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