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