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