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