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