← Functions lesson New set →

App Dev Worksheet: Functions

App development lesson 20: Functions · Set 830 · 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.🧰 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. 2.🧰 In function tip(bill), what is bill? (a) a loop (b) a screen (c) a parameter (d) an event
  3. 3.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(5));
  4. 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. 5.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(16));
  6. 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. 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. 8.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(0));
  9. 9.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(70, 5));
  10. 10.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(36));
  11. 11.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(2) + points(0));
  12. 12.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(8));
  13. 13.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(24));
  14. 14.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(5));
  15. 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. 16.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(2) + points(5));
  17. 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. 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. 19.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(10));
  20. 20.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(1) + points(5));
  21. 21.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(85, 3));
  22. 22.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(32));
  23. 23.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(4));
  24. 24.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(14));
  25. 25.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(3) + points(0));
  26. 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. 27.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(10, 2));
  28. 28.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(5));
  29. 29.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(50, 5));
  30. 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. 31.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(1));
  32. 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. 33.🧰 What number does this code show?
    function double(n) {
      return n * 2;
    }
    show(double(6));
  34. 34.🧰 Why write a function? (a) to hide errors (b) to slow the app (c) to make code longer (d) to reuse code
  35. 35.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(100, 5));
  36. 36.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(2));
  37. 37.🧰 What number does this code show?
    function total(price, qty) {
      return price * qty;
    }
    show(total(30, 4));
  38. 38.🧰 What number does this code show?
    function triple(n) {
      return n * 3;
    }
    show(triple(40));
  39. 39.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(4) + points(3));
  40. 40.🧰 What number does this code show?
    function points(stars) {
      return stars * 10 + 5;
    }
    show(points(2) + points(2));

Answer key

  1. sends back a value
  2. a parameter
  3. 10
  4. 60
  5. 48
  6. 60
  7. Hi, Riya!
  8. 50
  9. 350
  10. 108
  11. 30
  12. 16
  13. 72
  14. 15
  15. Hello, Kabir!
  16. 80
  17. 100
  18. a named block of code
  19. 20
  20. 70
  21. 255
  22. 64
  23. 90
  24. 42
  25. 40
  26. 40
  27. 20
  28. 100
  29. 250
  30. 0
  31. 60
  32. Well done, Aman!
  33. 12
  34. to reuse code
  35. 500
  36. 70
  37. 120
  38. 120
  39. 80
  40. 50

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