What is function in c?

A function in c programming is a set of statements used to perform some task. A collection of these functions build a program.

Every c program must have main() function.

Defining a function

return_type function_name(parameter list){
  function body
} 

A function definition in C consist of,

  • Return type
  • Function name
  • Parameters
  • Function body

Return type

A function may return a value. A return type is the data type of the value that function returns.

Function name

This is the actual name of the function.

Parameters

Parameters are optional. When the function is called one passes the value as a parameter and this value is termed as an actual parameter.

Function body

The function body contains set of statements to execute the task.


Aspects of function

Function declaration

Before using a function, one must declare it to tell the compiler about function name, function parameters and return type.

Function call

Function can be called from anywhere in the program. Note that the number of formal parameters and actual parameters must be same in number and should be in same order.

Function definition

It contains the actual statements which are to be executed. Note that only one value can be returned from the function.


Types of function

There are two types of functions in C, they are

Library functions

These are the built-in functions defined in C header files. For example, printf(), scanf(),..

User-defined functions

These are the functions created by the programmer.


Calling a function

To use the function to perform some task you have to call it. When a program calls a function, the control is transferred to the called function. The called function performs the task. When the control reaches the end of a function, i.e. closing curly bracket(}), it returns the control to the main function.

To call a function, you need to pass the required parameters along with the function name.


Return value

A function when it is called, may or may not return a value. If the function does not return any value then use void as return type otherwise depending upon the return value you need to specify the data type like int, float,…


Example to Illustrate C Functions

//C Function to add 2 numbers
#include<stdio.h>
int add(int a, int b)
{
  int c;
  c=a+b;
  return c;
}
int main()
{
  int result;
  printf("The function example \n");
  result=add(30,50);
  printf("The result is %d \n",result);
  return 0;
}

Output

C Function to add 2 numbers

Aspects of Function Calling

There are four types of function calling and they are as follows.

  • Function without arguments and without a return value.
  • Function without argument and with the return value.
  • Function with argument and without a return value.
  • Function with argument and with a return value.

Function without Arguments and Without a Return Value

// C program to illustrate function withot arguments and without a return value.
#include<stdio.h>
void printresult(){
 printf("Scholarsoul \n");
}

int main()
{
  printf("Example for function without argument and return value\n");
  printresult();
  return 0;
}

Output


Function without Argument and with Return value

// C program to illustrate function without argument and with the return value
#include<stdio.h>
int add()
{
  int a,b,res;
  printf("Enter two numbers \n");
  scanf("%d %d",&a,&b);
  res=a+b;
  return res;
}
int main()
{
  int result;
  printf("Program to illustrate function without argument and with return value\n");
  result=add();
  printf("Result is %d \n",result);
  return 0;
}

Output


Function with Argument and Without a return value

// C program to illustrate function with argument and withot a return vallue
#include<stdio.h>
void add(int a,int b)
{
  int c;
  c=a+b;
  printf("Result is %d \n",c);
}
int main()
{
 int a,b;
 printf("Enter the values for a and b\n");
 scanf("%d %d",&a,&b);
 add(a,b);
 return 0;
} 

Output


Function with Argument and with a Return value.

// C program to illustrate function with argument and with a return value
#include<stdio.h>
int add(int a,int b)
{
  int res;
  res=a+b;
  return res;
}
int main()
{
  int a,b,result;
  printf("Example for function with argument and return value \n");
  printf("Enter the values for a and b\n");
  scanf("%d %d",&a,&b);
  result=add(a,b);
  printf("result is %d \n",result);
  return 0;
}

Output


C Library Functions

Library functions are the inbuilt functions in C. These functions are created by the designers of compilers. To use these library functions, one has to include the appropriate header files in the program. All C library functions are defined in different header files and saved with the .h extension.

The list of mostly used header files

SL noHeader fileDescription
1stdio.hThis is a standard input/output file.
2conio.hThis is a console input/output header file.
3string.hIt contains all string-related library functions.
4math.hIt contains all the math operation-related functions.
5stdlib.hIt contains all the general library functions.
6ctype.hIt contains all character handling functions.

People also ask for

What are functions in c?

A function is a set of statements used to perform some task. A collection of these functions build a program.