Page 75 - 6437
P. 75

Calling a Function
                          While creating a C function, you give a definition of what the function has to do. To use a
                   function, you will have to call that function to perform the defined task.
                          When a program calls a function, the program control is transferred to the called function.
                   A called function performs a defined task and when its return statement is executed or when its
                   function-ending closing brace is reached, it returns the program control back to the main program.
                          To  call  a  function,  you  simply  need  to  pass  the  required  parameters  along  with  the
                   function  name, and  if the  function returns a  value, then  you can store the returned value. For
                   example:


                 #include <stdio.h>


                 /* function declaration */

                 int max(int num1, int num2);

                 int main ()

                 {
                    /* local variable definition */

                    int a = 100;
                    int b = 200;

                    int ret;


                    /* calling a function to get max value */
                    ret = max(a, b);


                    printf( "Max value is : %d\n", ret );


                    return 0;

                 }


                 /* function returning the max between two numbers */
                 int max(int num1, int num2)

                 {
                    /* local variable declaration */

                    int result;








                                                                                                      78
   70   71   72   73   74   75   76   77   78   79   80