← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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