Function is set of instructions to carry out a particular task.
A C program has at least one function main( ). Without main() function, there is technically no C program.
A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.
To overcome all the difficulties we use Function in C programming.
There are two types of functions in C on basis of whether it is defined by user or not.
Library functions are the pre-defined function in C programming language.
There are many library functions available in C programming to help the programmer to write a good efficient program.
In C programming you can copy string by just using strcpy() function which is defined under header file
"string.h".
These are some other library functions defined in "string.h"
C provides programmer to define their own function according to their requirement is known as "user defined functions".
Syntax:
Funtion_type function_name(parameter list);
A function declaration consists of four parts. They are,
Example:
int add(int a, int b);
Invoking(calling) of a function is called "function call".
Syntax:
function_name(Parameter name);
Example:
add(a,b);
The program module that is written to achieve a specific task is called "function definition".
Syntax:
Function Type Function name (Parameter List) { Local declaration; Function statements; Return statements; }
The function header consists of three parts:
function type,
function name
list of
parameter.
The function type specifies the type of value (like int or float) that the function is expected to return to the calling program. If the return type is not specified then C will assume that it is an integer type(int).
The function name is any valid C identifier and the name should be appropriate to the task performed by the function.
The parameter list declares the variables. In this variables will store the received data sent by the calling program. They serve as input data to the function to carry out the specific task.
Example:
1) Float sub (float x, float y) 2) int mul (int a, int b)
Function body is inside the curly braces. It contain Following three parts :
It is a statement that specify the variables.
It is a statement that perform the task of the function.
It is a statement that returns the value evaluated by the function.
Syntax with Example:
int add(int a, int b) { int sum; sum=a+b; return sum; }
#include<stdio.h>
#include<conio.h>
//Function declaration
int sum(int a, int b);
void main()
{
int a,b,x;
clrscr();
printf("Enter any two number of type integer/n");
scanf("%d%d"&a,&b);
// Function call
x=sum(a,b);
printf("Sum of two numbers=%d",x);
getch();
}
// Function Definition
int sum(int a, int b)
{
int sum;
sum=a+b;
return(sum);
}
Output-
Enter any two number of type integer 10 11 Sum of two numbers=21
On the basis of parameters and return values, the functions declaration classified into following types-
In this method, there is no data transfer between the calling function and called function.Return type is void. So this returns nothing.
#include<stdio.h>
#include<conio.h>
void swap();
void main()
{
clrscr();
swap();
getch();
}
void swap()
{
int num1.num2,temp;
num1=10;
num2=11;
temp=num1;
num1=num2;
num2=temp;
printf("num1=%d" and num2=%d,num1,num2);
}
Output-
num1=11 and num2-10
In this method, there is no data transfer between the calling function and called function.Here we won’t pass any arguments to the function while defining, declaring or calling the function.
#include<stdio.h>
#include<conio.h>
int max();
void main()
{
int y;
clrscr();
y=max();
printf("max=%d",y);
getch();
}
int max()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
{
return(a);
}
else
{
return(b);
}
}
Output-
Enter any two numbers 10 11 max=11
In this method, we pass the arguments to the function while calling the function. But, This type of function will not return any value when we call the function from main() function.
#include<stdio.h>
#include<conio.h>
void min(int a,int b);
void main()
{
int a,b;
clrscr();
printf("Enter any two numbers");
scanf("%d%d",&a,&b);
min(a,b);
getch();
}
void min(int a,int b)
{
if(a < b)
{
printf("%d is minimum",a);
}
else
{
printf("%d is minimum",b);
}
}
Output-
Enter any two numbers 10 11 10 is minimum
In this method, it allows us to pass the arguments to the function while calling the function. This type of function will return some value when we call the function from main() function.
#include<stdio.h>
#include<conio.h>
int avg(int a,int b,int c);
void main()
{
int a,b,c,A;
clrscr();
printf("enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
A=avg(a,b,c);
printf("average=%d",A);
getch();
}
int avg(int a,int b,int c)
{
int average;
average=(a+b+c)/3;
return(average);
}
Output-
enter three numbers 11 10 9 average=10
In this method, the value of the actual parameters is copied into the formal parameters.
The actual parameter is the argument used in the function call whereas and parameter is the argument which
is used in the function definition. Here we can't modify the value of the actual parameter by the formal
parameter.
By default, C programming uses call by value to pass arguments.
#include<stdio.h>
int sum(int a, int b)
{
int c=a+b;
return c;
}
int main(
{
int var1 =10;
int var2 = 20;
int var3 = sum(var1, var2);
printf("sum=%d", var3);
return 0;
}
Output-
sum=30
[We will learn this concept in pointer tutorial.]