← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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