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