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