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