Functions

Exercises

  1. Write a function named Sub which takes two arguments of type double, and returns the result of subtracting the second one from the first one. Use this function to calculate 3.2 – 8 and write out the result. The result should be: -4.8.
  2. Write a program that, by using only the functions written so far and without using any variables or arithmetic operators, calculates the result of the following expressions:
    • a) 1 + 3 + 5
      (the result must be 9)
    • b) 1 + 3 + 5 + 7 + 4
      (the result must be 20)
    • c) 10 – (3 + 5)
      (the result must be 2)
    • d) 10 – 3×5
      (the result must be -5)
    • e) (10 – 3)×5
      (the result must be 35)
    • f) 11 – 5 – 2
      (the result must be 4)
    • g) 2×16 – 4×7 + 2×(4 – 3 – 2)
      (the result must be 2). Hint is in the footnote[*].

Click here for solutions