← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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