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