← Functions lesson New set →

App Dev Worksheet: Functions

App development lesson 20: Functions · Set 501 · 40 questions
TalentJR

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

Answer key

  1. to reuse code
  2. a parameter
  3. sends back a value
  4. 24
  5. 60
  6. 40
  7. 14
  8. 9
  9. 60
  10. 80
  11. 60
  12. 8
  13. 18
  14. 39
  15. 125
  16. 40
  17. 380
  18. 46
  19. 16
  20. Hi, Riya!
  21. 52
  22. Hello, Kabir!
  23. 60
  24. 50
  25. Well done, Kabir!
  26. greet()
  27. Welcome, Zoya!
  28. Hi, Kabir!
  29. 60
  30. 34
  31. Welcome, Riya!
  32. 28
  33. a named block of code
  34. 160
  35. 275
  36. 84
  37. 0
  38. 40
  39. 80
  40. 10

Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students