We now study the simplest type of user defined functions those that display one

We now study the simplest type of user defined

This preview shows page 10 - 16 out of 19 pages.

We now study the simplest type of user-defined functions – those that display one or more lines of output. These are useful for tasks such as displaying instructions to the user on how to use a program.
Image of page 10
11 Example … /* Performs three square root computations */ #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ void instruct (void); //displays user instruction int main(void) { double first, second, /* input - two data values */ first_sqrt, /* output - square root of first input */ second_sqrt, /* output - square root of second input */ sum_sqrt; /* output - square root of sum */ /* Display instrctions */ instruct(); printf("Enter a number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("Square root of the number is %.2f\n", first_sqrt); // continue next slide …
Image of page 11
12 Example // continue from previous slide printf("Enter a second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("Square root of the second number is %.2f\n", second_sqrt); sum_sqrt = sqrt(first + second); printf("Square root of the sum of the 2 numbers is %.2f\n",sum_sqrt); return (0); } // end of main function /* displays user instructions */ void instruct(void) { printf("This program demostrates the use of the \n"); printf("math library function sqrt (square root). \n"); printf("you will be asked to enter two numbers -- \n"); printf("the program will display the square root of \n"); printf("each number and the square root of their sum. \n\n"); }
Image of page 12
13 Function Prototypes Like other identifiers in C, a function must be declared before it can be used in a program. To do this, you can add a function prototype before main to tell the compiler what functions you are planning to use. A function prototype tells the C compiler: 1. The data type the function will return For example, the sqrt function returns a type of double. 2. The function name 3. Information about the arguments that the function expects. The sqrt function expects a double argument. So the function prototype for sqrt would be: double sqrt(double);
Image of page 13
14 Function Prototypes : void Functions void instruct(void); is a void function Void function - does not return a value The function just does something without communicating anything back to its caller. If the arguments are void as well, it means the function doesn’t take any arguments. Now, we can understand what our main function means: int main(void) This means that the function main takes no arguments, and returns an int
Image of page 14
15 Function Definition The prototype tells the compiler what arguments the function takes and what it returns, but not what it does.
Image of page 15
Image of page 16

You've reached the end of your free preview.

Want to read all 19 pages?

  • Spring '10
  • baleh
  • Return statement, Continuous function, Void type

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture