← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. to reuse code
  2. 24
  3. 50
  4. 50
  5. 33
  6. a named block of code
  7. sends back a value
  8. Hi, Arjun!
  9. 36
  10. greet()
  11. Hi, Zoya!
  12. 60
  13. 50
  14. 200
  15. Well done, Zoya!
  16. 120
  17. a parameter
  18. Welcome, Aman!
  19. 90
  20. 38
  21. 50
  22. 40
  23. 50
  24. 510
  25. 90
  26. 60
  27. 160
  28. 70
  29. 60
  30. 20
  31. 20
  32. 60
  33. Hello, Arjun!
  34. 20
  35. 40
  36. 50
  37. 51
  38. 70
  39. 117
  40. 80

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