← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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