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