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