← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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