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