Types of Function in C

In C language there are two types of functions:

  • System Defined Function
  • User Defined Function

Please refer to the function in C for a better understanding of the sections below.

System Defined Functions:

These are pre-defined functions which are already defined by the system in the respective header files like stdio.h, conio.h, math.h.

The header files must be included before using system-defined functions. They are also known as Library functions or Standard functions or Pre-defined functions.

A few examples like scanf() and printf() functions which are defined in the header file named “stdio.h”.

Note:

  • System defined functions are declared in header files.
  • These functions are implemented in .dll files.(DLL stands fir Dynamic Link Library).
  • To use the system defined functions, the respective header files must be included.

User Defined Functions:

User defined functions are implemented by the user. For example, the main() function is implemented by the user so it is called user defined functions.

Sample Program:

#include <stdio.h>

int sum (int num1, int num2) /* function definition */
{
  return num1 + num2;
}

int main ()
{
  int a = 10;
  int b = 20;
  int res = sum (a, b); /* function call */
  printf ("\n The sum is %d\n", res);
  return 0;
}

In the concept of functions, the function call is known as “Calling Function” and the function definition is called “Called Function“.

When a function call is made, the control of execution jumps to the called functions. After the call function execution is over, the control comes back to the caller function.

When the control jumps from the calling function to the called function it may carry one more data value called parameters (actual parameters) and while the control resumes to the called function it may return a single value with the help of the keyword “return”.

Based on the data flow between the calling function and the called function, the function can be classified as follows:

  • Function without parameters and without return value.
  • Function with parameters without return value.
  • Function without parameters and return value.
  • Function with parameters and with return value.

Function without parameters and without return value:

In this type of function, no data is transferred from calling function to called function nor there is any data returned from the calling function to the called function.

Sample Program:

#include <stdio.h>

void sum()
{
  int a = 10;
  int b = 20;
  int sum = a + b;
  printf ("\n The sum is %d\n", sum);
}

int main ()
{
  /* calling the function */
  sum();
  return 0;
}

Function with parameters without return value:

In this type of function, data is transferred from calling function to called function but no data is returned from the calling function to called function.

Sample Program:

#include <stdio.h>

int sum (int num1, int num2) /* function definition */
{
  printf ("\n The sum is %d\n", num1 + num2);
}

int main ()
{
  int a = 10;
  int b = 20;
  sum (a, b); /* function call */
  return 0;
}

Function without parameters and return value:

In this type of function, no data is transferred from calling function to called function but data is returned from the calling function from called function.

Sample Program:

#include <stdio.h>

void sum()
{
  int a = 10;
  int b = 20;
  int sum = a + b;
  return sum;
}

int main ()
{
  /* calling the function */
  int res = sum();
  printf ("\n The sum is %d\n", res);

  return 0;
}

Function with parameters and with return value:

In this type of function, both data is transferred from calling function to called function and data returned from the calling function to called function.

Sample Program:

#include <stdio.h>

int sum (int num1, int num2) /* function definition */
{
  return num1 + num2;
}

int main ()
{
  int a = 10;
  int b = 20;
  int res = sum (a, b); /* function call */
  printf ("\n The sum is %d\n", res);
  return 0;
}

Note:

  • The parameters specified in the calling functions are known as Actual parameters.
  • The parameters specified in called functions are known as Formal parameters.
  • The value of the actual parameter is copied into formal parameters in case of call-by-value and the address of the actual parameter is copied into formal parameters if the calling type is call-by-reference.
  • call-by-value and call-by-reference are two methods of passing parameters to function. Details in the next section.

Relevant Posts:



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: