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