← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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