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