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