Function categories

When a function is called, parameters in the called function are bound to the corresponding arguments supplied by the calling function. Before calling a function, it must be declared with a prototype of its parameters inside the main(). The general form for a function declaration is

function-type function-name (parameter type list);

where the function-type and function-name are the type and name of the declared function respectively. The called function may accept arguments from the calling function and may return values to the calling function, depending on the situation.

Function with no arguments and no return values

Example for this type of function is given below. Here, the main() (calling function) calls two functions namely printlin () and value() . Upon a call, the program control passes from the calling function to the called function, and execution begins from the first executable statement of the called function. The called function is executed until a return or closing brace of the function is encountered, at which point the control passes back to the point atter the function call. The functions printline() and value() accept no arguments from main() and they also return no value to it.

Example 5.6

#include <stdio.h>
int main()
{
    printline();      /* call to printline() that accepts no arguments */
    value();           /* call to value() that accept no arguments */
    printline();     /* another call to printline() */
} 

/* functionl: printline ( ) */

printline()
{
      int i;
     for (i=0; i<80; i++) 
     {
         print f ("-");
      }
}

/* function2: value () */
value()
{
    int number1, number2;
    int result;
    printf("Enter the values..\n"); 
    scanf("%d%d", &number1, &number2) ; 
    result = number1 / number2:
    printf("The result is %d\n", result);
}

Function with arguments but no return values

In the example given below, the function interest () is called from main() and it accepts three arguments

Example 5.7

The function interest() is supplied with three arguments namely principal, rate and period. This function calculates the simple interest based on the values sent to it, but it does not return the value of simple interest to the calling function. When the function is called from the calling function, control passes to the function, the result is evaluated, and later displayed.

#include <stdio.h>
int main()
{
    float interest (float, float, int); /* Function prototyping */ 
    float principal, rate; 
    int period; 
    printf( "Enter principal, amount and interest ");
    scanf ( "%f%f", &principal, &rate); 
    printf("Enter the number of years");
    scanf ( "%d", period);
    interest (principal, rate, period);
}

float interest (float p, float r, int n) 
{
    float simple; 
    simple = (p*n*r) /100;  
    printf ("The simple interest for a period of %d years is %f", n, simple):
}

Function with arguments and return values

In the following example, the function square () accepts one argument and rectums the result of type float to the calling function main().

Example 5.8

The example here, illustrates how the function square() accepts the number and manipulates the square of that number. When this function is called from main(), the control passes to the called function, square of the number is evaluated and the result is returned to the environment in which the function is called

#include <stdio.h>
int main ()
{
    int number;
    float result;
    float square (int); /* 7unction prototyping /
    printf ("Enter the number whose square has to be found\n");
    scanf ( "%d", &number) ;
    result = square(number); 
   printf ("The square root of the number is %f", result); 
}

float square (int num)
{
    float res; 
    res = num * num;
    return (res);
}