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