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