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