Functions in C Programming With Examples

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.
A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

eg.

int max(int x, int y)
{
int big;
if (x>y)
  big = x;
else
  big = y;
return big;
}  

A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −


Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
Function Body − The function body contains a collection of statements that define what the function does.


Types of function : There are two types of function in C programming:

. Standard library functions
. User-defined functions


Standard library functions


The standard library functions are built-in functions in C programming.
These functions are defined in header files. For example,


 The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in the stdio.h header file.
Hence, to use the printf()function, we need to include the stdio.h header file using #include <stdio.h>.
 The sqrt() function calculates the square root of a number. The function is defined in the <math.h> header file.

You can also create functions as per your need. Such functions created by the user are known as user-defined functions.


Example 1 No Return, No Parameters/Arguments- void max ( )
Example 2 No Return, With Parameters/Arguments- void max ( int x, int y )
Example 3 With Return, No Parameters/Arguments- int max ( )
Example 4 With Return, With Parameters/Arguments- int max ( int x, int y )

Example 1: No arguments passed and no return value

#include <stdio.h&gt>
#include <conio.h>
void max();
void main()
{
  clrscr();
  max(); // argument is not passed
  getch();
}
// return type is void meaning doesn't return any value
void max()
{
  int x,y,n;
  printf("Enter 2 integers:");
  scanf("%d%d",&x,&y);
  if( x>y )
    n=x;
  else
    n=y;
  printf("%d is biggest number.", n);
}

Example 2: With arguments and no return value

#include <stdio.h>
#include <conio.h>
void max(int,int);
void main()
{
  int x,y;
  clrscr();
  printf("Enter 2 integers:");
  scanf("%d%d",&x,&y);
  max(x,y); // arguments are passed
  getch();
}
// return type is void meaning doesn't return any value
void max(int x,int y)
{
  int n;
  if( x>y )
    n=x;
  else
    n=y;
 printf("%d is biggest number.", n);
}

Example 3: No arguments passed and with return value

#include <stdio.h>
#include <conio.h>
int max();
void main()
{
  int n;
  clrscr();
  n=max(); // argument is not passed
  printf("%d is biggest number.", n);
  getch();
}
// return type is int meaning it return one integer value
int max()
{
  int n,x,y;
  printf("Enter 2 integers: ");
  scanf("%d%d",&x,&y);
  if( x>y )
    n=x;
  else
    n=y;
 return n;
}

Example 4: With arguments and with return value

#include <stdio.h>
#include <conio.h>
int max(int , int);
void main()
{
  int n,x,y;
  clrscr();
  printf("Enter 2 integers: ");
  scanf("%d%d,&x,&y);
  n=max(x,y); // arguments are passed
  printf("%d is biggest number.", n);
  getch();
}
// return type is int meaning it return one integer value
int max(int x, int y)
{
  int n;
  if( x>y )
    n=x;
  else
    n=y;
 return n;
}

Parameter Passing to functions


The parameters passed to function are called actual parameters. For example, in the above program 10 and 20 are actual parameters.
The parameters received by function are called formal parameters. For example, in the above program x and y are formal parameters.
There are two most popular ways to pass parameters.


Pass by Value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of caller.
Pass by Reference: Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller.

WAP in C to Calculate the factorial of a number

#include<stdio.h>
long int fact(int n);
void main()
{
  int n;
  long int x;
  printf("Enter a positive integer:");
  scanf("%d",&n);
  x=fact(n);
  printf("Factorial of %d = %ld", n, x);
  getch();
}
long int fact(int n)
{
  long int y=1;
  while(n>1)
    {
    y=y*n;
    n--;
    }
  return y;
}

Output

Enter a positive integer : 6
Factorial of 6 = 720

Why do we need functions?


 Functions help us in reducing code redundancy. If functionality is performed at multiple places in software, then rather than writing the same code, again and again, we create a function and call it everywhere. This also helps in maintenance as we have to change at one place if we make future changes to the functionality.


 Functions make code modular. Consider a big file having many lines of codes. It becomes really simple to read and use the code if the code is divided into functions.


 Functions provide abstraction. For example, we can use library functions without worrying about their internal working.

Advantages of user-defined function

  1. The program will be easier to understand, maintain and debug.
  2. Reusable codes that can be used in other programs
  3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.

Leave a Comment