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