← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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