← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 180
  2. sends back a value
  3. to reuse code
  4. 70
  5. 102
  6. 120
  7. 50
  8. 0
  9. 40
  10. 20
  11. 120
  12. 40
  13. 22
  14. 70
  15. 62
  16. a parameter
  17. 425
  18. Hello, Zoya!
  19. 510
  20. 60
  21. 76
  22. 90
  23. 60
  24. 10
  25. a named block of code
  26. 70
  27. 36
  28. Welcome, Arjun!
  29. 450
  30. 42
  31. 40
  32. 33
  33. 70
  34. 40
  35. 0
  36. greet()
  37. 40
  38. 50
  39. 50
  40. 46

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