← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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