![]() |
| Function in C Programming for BCA, BTech & Diploma Students |
Function Definition in C
Function: A function is a collection of statement that performs specific task. Function executes called by same function name. It can be used large program is divided into a small building of block and this building block is called function. We can call a function any where C program again and again. This is a most important features of function is code reusability and time saving be can not declared same property again and again.
Syntax of Function Declaration in C
return_type function_name(Parameter_List)
{
function_body;
}
function_name();
================================
void jiocoding()
{
printf("Welcome To Jiocoding");
}
jiocoding();
How to Write Function Description in C
Return type: A return type is a keyword indicates type of value is being returning by the function. If we do not to return any value then we can used void return type keyword in place of return type.
Function name: Function name is a user defined name but function name can not be matched any keywords name. It is the actual name of the function. It is also used at the time of calling the function.
parameter list: We can pass a number of parameter means are variables. These variables may be used in the program. The value of parameter can be initialize at the time of calling of function.
body: Function body can be used to the place the actual code or logic is written to perform the specific task.
How to Write Function in C Programming
#include<stdio.h>
#include<conio.h>
void message()
{
printf("Welcome To Jiocoding Website\n");
printf("Welcome To C Language");
}
void main()
{
clrscr();
message();
getch();
}
============OUTPUT============
Welcome To Jiocoding Website
Welcome To C LanguageTypes of Function in C Programming
1. Predefined Function: Predefined function is a define in C library that is called predefined function. In this function can not be used in function name because it is a reserved function name. Example printf(), scanf(), getch(), clrscr() and more it is also predefined function in C.
2. User defined Function: A user defined function is a create by user so it is called user defined. In this function define is a user choice like Add(), Sub(), Multi(), Div(), max() and more function can be created by user.
Categories of Function in C Programming
1. Function with no return type and no parameter.
2. Function with no return type and with parameter.
3. Function with return type and no parameter.
4. Function with return type and with parameter.
Function with no return type and no parameter: This function can be used no parameter and no value return by function that is called function with no return type and no parameter.
Example of function with no argument and no return value
#include<stdio.h>
#include<conio.h>
void addition()
{
int x=10,y=50;
printf("Addition of x+y:%d",(x+y));
}
void main()
{
clrscr();
addition();
getch();
}
============OUTPUT============
Addition of x+y:60Function with no return type and with parameter: This function can be used to inside the parameter and no return type by function that is called by function with no return type and with parameter.
Example of function with argument and no return value
#include<stdio.h>
#include<conio.h>
void addition(int x, int y)
{
printf("Addition of x+y:%d",(x+y));
}
void main()
{
clrscr();
addition(40,80);
getch();
}
============OUTPUT============
Addition of x+y:120Function with return type and no parameter: This function can be used no parameter and some value return that is called by function with return type and no parameter.
Example of function with no argument and return value
#include<stdio.h>
#include<conio.h>
int addition()
{
int x=50,y=80,z;
z=x+y;
return z;
}
void main()
{
clrscr();
printf("Value of z:%d",addition());
getch();
}
============OUTPUT============
Value of z:130Function with return type and with parameter: This function can be used both operation with parameter and with return type to take some parameter and some value return that is called by function with return type and with parameter.
Function with argument and return value example
#include<stdio.h>
#include<conio.h>
int addition(int x, int y)
{
int z=x+y;
return z;
}
void main()
{
clrscr();
int a=60,b=80;
int sum=addition(a,b);
printf("Result is:%d",sum);
getch();
}
============OUTPUT============
Result is:140Explain Call by Value in C Programming
Function Call by Value: A call by value can be used to passing arguments to a function that arguments copies of a actual values are passed to the function arguments. You can any changes made to the arguments inside the function do not reflect the original variables. When you can calling a function than you can direct value is passed at the time of function calling.
In this below example we can use direct value is passed at the time of function calling. Here a is actual arguments and x is formal arguments.
Call by Value Program in C with Output
#include<stdio.h>
#include<conio.h>
void jiocoding(int x)//function defination
{
x=x+100;
}
void main()
{
clrscr();
int a=50;
printf("Before Calling a:%d\n",a);
jiocoding(a); //function calling
printf("After Calling a:%d\n",a);
getch();
}
============OUTPUT============
Before Calling a:50
After Calling a:50Call by Reference in C Programming
Function Call by Reference: A call by reference is a function of passing arguments to a function using the memory addresses using pointer of the actual parameter. In this calling a function reference of the value is passed at the time of function calling. It can be allows the function to directly access and change the original value in the time of function calling.
Reference is also called the address of variable. When address of value is passed at the time of calling so it is important to use pointer in the place of parameter.
Call by Value Program in C with Output
#include<stdio.h>
#include<conio.h>
void jiocoding(int *x)//function defination
{
*x=*x+100;
}
void main()
{
clrscr();
int a=50;
printf("Before Calling a:%d\n",a);
jiocoding(&a); //function calling
printf("After Calling a:%d\n",a);
getch();
}
============OUTPUT============
Before Calling a:50
After Calling a:150Function with default arguments
A function with default value can be used to contains a number of arguments with some initialized value at the time of function calling. If you can not defines the value at the runtime of C program than compiler used to default value.
Explain default argument with Example
#include<stdio.h>
#include<conio.h>
void jiocoding(int x=80,int y=80)//function defination
{
printf("Addition of x+y:%d\n",(x+y));
}
void main()
{
clrscr();
int x=50,y=60;
printf("Without Calling a Value\n");
jiocoding(); //function calling
printf("With Calling a Value");
jiocoding(x,y);
getch();
}
============OUTPUT============
Without Calling a Value
Addition of x+y:160
With Calling a Value
Addition of x+y:110Passing One Dimensional Array to Function in C
A passing of array used by function this there is an array in the place of parameter and array value is passed at the time of function calling.
Explain default argument with Example
#include<stdio.h>
#include<conio.h>
void jiocoding(int a[5])//function definition
{
for(int i=0;i<5;i++)
printf("Array element in Function [%d]:%d\n",i,a[i]);
}
void main()
{
clrscr();
int a[5]={10,20,50,40,60};
jiocoding(a); //function calling
getch();
}
============OUTPUT============
Array element in Function [0]:10
Array element in Function [1]:20
Array element in Function [2]:50
Array element in Function [3]:40
Array element in Function [4]:60Recursion Function in C
Recursion: A recursion can be used to process of calling a function by itself is that is called recursion and this function that calls itself is called recursive function.
Example of recursion in C
#include<stdio.h>
#include<conio.h>
void factorial(int n,int f)//function definition
{
if(n>=1){
f=f*n;
n--;
factorial(n,f);
}
else
printf("Factorial is:%d",f);
}
void main()
{
clrscr();
int n;
printf("Enter any number:");
scanf("%d",&n);
factorial(n,1); //function calling
getch();
}
============OUTPUT============
Enter any number:5
Factorial is:120
