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