← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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