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