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