← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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