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