← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

  1. 90
  2. to reuse code
  3. 20
  4. 40
  5. 54
  6. a named block of code
  7. 40
  8. 80
  9. 30
  10. 42
  11. greet()
  12. 40
  13. 60
  14. 30
  15. Hi, Arjun!
  16. 0
  17. 52
  18. 50
  19. a parameter
  20. 60
  21. 100
  22. 50
  23. 96
  24. 40
  25. 240
  26. sends back a value
  27. 420
  28. 225
  29. 8
  30. Hi, Zoya!
  31. 18
  32. 90
  33. 70
  34. 80
  35. 90
  36. 110
  37. 78
  38. Hi, Kabir!
  39. 250
  40. Hello, Aman!

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