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