Calling of functions in C – Free C programming Tutorials

In this tutorial you will familiarize with the function declaration,definition of functions and calling of a function along with the examples.

Function Declaration :

A function declaration tells the compiler about the function name and how to call the function.

Syntax for function declaration is given below :
return_type function_name( parameters\arguments list );

For the above defined function min(), the function declaration is as follows:
int min(int num1, int num2);

Let us analyse the above code:

int is return type of the function.
min is name of the function.
num1 and num2 are arguments\parameters of the function of data type int.

Example of defining a function:

Given below is the basic code for a function called min().
This function takes two parameters/arguments num1 and num2 and returns the minimum value between the two given inputs.

/* function returning the min between two numbers */
// Declaration of function
int min(int num1, int num2)
{
   /* variable declaration */
    int result;

    if (num1 < num2)    // condition if-else    
       result = num1;
    
    else
      result = num2;

    return result;
}

If num1=10 and num2 =20
then output :
10 
which is min number among 10 and 20.

Calling a Function in C:

After declaring a C function, you have to give a definition of the function’s operation i.e. the specific task perform by the function. To use a function in the main() function, you have to call that function to perform the defined task.
When a program invokes/calls a function, the program control is transferred to the invoked function.
A called function performs a defined task and when its function ending closing brace (}) is reached, it returns the program control back to the main program and return value to the main function.
For calling of a function, you basically need to pass the function name along with the requisite parameters/arguments.
And if the function returns a value, then you can store the returned value.

For example:

#include <stdio.h>

/* function declaration */
int min (int num1, int num2);

int main ()
{
 /* local variable definition */
   int a = 10;
   int b = 20;
   int result;

/* calling a function to get min value as output */
   
   result = min(a, b);

   printf( "min value is : %d\n", result );

   return 0;
}



/* Definition of function returning the min between two numbers */

int min(int num1, int num2)
{
 /* local variable declaration */
     int res;

     if (num1 < num2)

       res = num1;

     else

      res = num2;

    return res;

}

After running final executable code, it would produce the following result:

OUTPUT

Min value is : 10

Function Arguments

Parameters/arguments are used by functions to pass information in and out.

There are two types of function arguments:

  1. FORMAL PARAMETERS and
  2. ACTUAL PARAMETERS
  • Formal parameters :  The parameters which are used in the function definition are called the formal  parameters.
  • Actual parameters : The parameters which are used in the function call are called the actual parameters.

If you have any doubt about the context please comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *