08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 ■ Functions

A sample run is as follows:

Enter first length: 3m 75cm

Enter second length: 5m 50cm

Sum is 9m 25cm

You are encouraged to do the following:

• Modify getInt so that it works for negative integers.

• Write a function getDouble, which returns the next floating-point number

in the input. It should work even if the next number does not contain a

decimal point.

EXERCISES 7

1. Explain why functions are important in writing a program.

2. Given the function header

void test(int n)

explain carefully what happens when the call test(5) is made.

3. Given the function header

double fun(int n)

explain carefully what happens when the following statement is executed:

printf("The answer is %f\n", fun(9));

4. Given the function header

void test(int m, int n, double x)

say whether each of the following calls is valid or invalid. If invalid, state why.

test(1, 2, 3);

test(-1, 0.0, 3.5);

test(7, 2);

test(14, '7', 3.14);

5. Write a function sqr, which given an integer n, returns n2.

6. Write a function isEven, which given an integer n, returns 1 if n is even and 0 if n

is odd.

7. Write a function isOdd, which given an integer n, returns 1 if n is odd and 0 if

n is even.

193

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!