← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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