← Functions lesson New set →

App Dev Worksheet: Functions

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

Answer key

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

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