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