![]() |
How To Use C Functions Explain Briefly Details |
How To Use C Functions Explain Briefly Details
Function: A function is a collection of statements that performs a specific task. It executes when it is called by its name.
C program to create functions add for two different data types.
A large program is divided into several small building blocks for simplicity and this building block is called function.We can call a function again and again.
The most important feature of function is code reusability.
The most important feature of function is code reusability.
The C library provides many pre-defined functions.
Function Declaration in C
Return Type: A operation could come at a price. The return_type is that the knowledge variety of the worth the operate returns.
Some functions perform the required operations while not returning a price.
Function Name: This is that the actual name of the operation.
The operating name and also the argument(parameter) list along represent the operate signature.
Arguments: A parameter is sort of a placeholder. once an operation is invoked, you pass a price to the parameter.
This price is mentioned as an actual parameter or argument. The parameter list refers to the sort, order, and range of the parameters of an operation.
Parameters square measure optional; that's, an operation could contain no parameters.
Function Body: The operating body contains a group of statements that outline what the operation will do.
Return Type: A operation could come at a price. The return_type is that the knowledge variety of the worth the operate returns.
Some functions perform the required operations while not returning a price.
Function Name: This is that the actual name of the operation.
Function Name: This is that the actual name of the operation.
The operating name and also the argument(parameter) list along represent the operate signature.
Arguments: A parameter is sort of a placeholder. once an operation is invoked, you pass a price to the parameter.
Arguments: A parameter is sort of a placeholder. once an operation is invoked, you pass a price to the parameter.
This price is mentioned as an actual parameter or argument. The parameter list refers to the sort, order, and range of the parameters of an operation.
Parameters square measure optional; that's, an operation could contain no parameters.
Function Body: The operating body contains a group of statements that outline what the operation will do.
Function Body: The operating body contains a group of statements that outline what the operation will do.
Syntax of Function in C
return_Type Function_Name(Parameter List)
{
Statement;
}
return_Type Function_Name(Parameter List)
{
Statement;
}
Key Point About The Function.
void data();
A operate declaration during which void(no come type) indicates there's no price returning by this operate and data is that the name of operations.
How To Use Function in C?
#include<stdio.h>
void data()
{
int x=10;
int y=20;
int z=x+y;
printf("Addition:%d",z);
}
#include<stdio.h>
void data()
{
int x=10;
int y=20;
int z=x+y;
printf("Addition:%d",z);
}
This is an operation definition and here we can see that code is written to perform the addition task.
Function Calling: At this stage, the function is termed.
data();
To decision a perform simply write perform name and place semi-colon(;).
How To Use Function in C?
#include<stdio.h>
void data();
{
int x=100;
int y=200;
int z=x+y;
printf("Addition of data:%d",z);
}
int main()
{
data();
}
*****OUTPUT*****
Addition of data:300
#include<stdio.h>
void data();
{
int x=100;
int y=200;
int z=x+y;
printf("Addition of data:%d",z);
}
int main()
{
data();
}
*****OUTPUT*****
Addition of data:300
Predefined Function in C
A predefined function in the library in C Language is called a predefined function.printf()
scanf()
clrscr()
getch()
A User-defined function that is made by the user is called a user-defined function.
Userdefined Function in C
A User-defined function that is made by the user is called a user-defined function.
Addition()
Subtraction()
Multiplication()
Division()
There are four categories of user-defined function
1. operate with no come kind and no parameter
2. Function is used with no return type and with parameter.
3. Function with return type and no parameter.
4. operate with come kind and with parameters.
Some Others Programming Language Functions
👉C++ Language Function
👉Java Language Function
👉Python Language Function
Function no return type and no parameter
The operation during which there's no parameter and there's no worth coming by that operation is termed operate with no return kind and no parameter.
How To Use No Return Type No Parameter Function in C
#include<stdio.h>
void data();
{
int x=20;
int y=20;
int z=x*y;
printf("Multiplication of data:%d",z);
}
int main()
{
data();
}
*****OUTPUT*****
Multiplication of data:400
#include<stdio.h>
void data();
{
int x=20;
int y=20;
int z=x*y;
printf("Multiplication of data:%d",z);
}
int main()
{
data();
}
*****OUTPUT*****
Multiplication of data:400
Function with no return type and with parameter
The operation during which there's no parameter and there's some worth coming by that operation is termed operate with return kind and no parameter.How To Use No Return Type With Parameter Function in C
#include<stdio.h>
void data(int x,int y);
{
int z=x+y;
printf("Addition of data:%d",z);
}
int main()
{
data(100,100);
}
*****OUTPUT*****
Addition of data:200
#include<stdio.h>
void data(int x,int y);
{
int z=x+y;
printf("Addition of data:%d",z);
}
int main()
{
data(100,100);
}
*****OUTPUT*****
Addition of data:200
In the above example, there are two parameters of integer type names x and y there at the time of calling two integer values will be passed in which first will assign to x and the second will assign to y.
variable z in-store data information.
The function in which there is no parameter and there is some value returning by that function is called Function with return type and no parameter.
Function with return type and no parameter
The function in which there is no parameter and there is some value returning by that function is called Function with return type and no parameter.
How To Use Return Type No Parameter Function in C
#include<stdio.h>
int data();
{
int x=100;
int y=100;
int z=x+y;
return z;
}
int main()
{
data();
printf("Addition of data:%d",z);
}
*****OUTPUT*****
Addition of data:200
#include<stdio.h>
int data();
{
int x=100;
int y=100;
int z=x+y;
return z;
}
int main()
{
data();
printf("Addition of data:%d",z);
}
*****OUTPUT*****
Addition of data:200
Above example, there is no parameter but the function will return an integer value because there is an int keyword in the place of return type and returned value will assign to variable z.
Function with return type and with parameter
The perform during which there's no parameter and there's some worth come backing by that perform is named perform with return kind and no parameter.How To Use Return Type With Parameter Function in C?
#include<stdio.h>
int data(int x,int y);
int data(int x,int y)
{
int z;
int z=x+y;
return z;
}
int main()
{
int a=data(100,100);
printf("Addition of data:%d",a);
}
*****OUTPUT*****
Addition of data:200
#include<stdio.h>
int data(int x,int y);
int data(int x,int y)
{
int z;
int z=x+y;
return z;
}
int main()
{
int a=data(100,100);
printf("Addition of data:%d",a);
}
*****OUTPUT*****
Addition of data:200
Above example, there are two parameters and the function will return an integer value because there is an int keyword in the place of return type and returned value will assign to variable a.
There is two way of calling a function.
Calling of Function
There is two way of calling a function.
1. Call By Value.
2. Call By Reference.
1. Call by Value: In this type of calling a function direct value is passed at the time of calling.
Indecision by worth the changes created informal parameters do not replicate in actual parameters.
How To Use Call By Value Function in C?
#include<stdio.h>
void data(int a)
{
a=a+100;
int main()
{
int x=100;
printf("Before Calling of data:%d",x);
data(x);
printf("After Calling of data:%d",x);
}
*****OUTPUT*****
Before Calling of data:100
After Calling of data:100
#include<stdio.h>
void data(int a)
{
a=a+100;
int main()
{
int x=100;
printf("Before Calling of data:%d",x);
data(x);
printf("After Calling of data:%d",x);
}
*****OUTPUT*****
Before Calling of data:100
After Calling of data:100
2. Call by Reference: In this type of calling a function, the reference of the value is passed at the time of calling. call by value the changes made informal parameters reflect in actual parameters. Reference is also called address.
When the address of information is passed at the time of business thus it's necessary to use a pointer within the place of a parameter.
For better understanding see the example below.
How To Use Call By Reference Function in C?
#include<stdio.h>
void data(int *a)
{
*a=*a+100;
int main()
{
int x=100;
printf("Before Calling of data:%d",x);
data(&x);
printf("After Calling of data:%d",x);
}
*****OUTPUT*****
Before Calling of data:100
After Calling of data:200
#include<stdio.h>
void data(int *a)
{
*a=*a+100;
int main()
{
int x=100;
printf("Before Calling of data:%d",x);
data(&x);
printf("After Calling of data:%d",x);
}
*****OUTPUT*****
Before Calling of data:100
After Calling of data:200
Function with the default value
default value: In this type of function ,the function contains a number of parameter with some initial value[for example:void data(int x=100,int y=200)].At the time of calling if there is no value is passed [for example: data();] then the default value will be x=100 and y=200,but if value is passed [for example: sum(20,10);] then the value will be x=20 and y=10.
How To Use Default Value Function in C?
#include<stdio.h>
void data(int x=100,int y=200)
{
int res=x+y;
printf("Adition of data:%d",res);
}
int main()
{
int x=100;
int y=200;
printf("Without value of Add data:\n");
data();
printf("With value of Add data:");
data(20,10);
}
*****OUTPUT*****
Addition of data:300
Without the value of Add data:300
With the value of Add data:30
#include<stdio.h>
void data(int x=100,int y=200)
{
int res=x+y;
printf("Adition of data:%d",res);
}
int main()
{
int x=100;
int y=200;
printf("Without value of Add data:\n");
data();
printf("With value of Add data:");
data(20,10);
}
*****OUTPUT*****
Addition of data:300
Without the value of Add data:300
With the value of Add data:30
Passing Array to Function
Array Function: In this type of function there is an array in the place of the parameter.
example: void data(int a[5])] and its value are passed at the time of calling.
How To Array Function in C?
#include<stdio.h>
void data(int a[5])
{
int r=0;
for(int i=0;i<5;i++)
r=r+a[i];
printf("Total Addition Element of data:%d",r);
}
int main()
{
int x[5]={80,77 14,78,01};
data(x);
}
*****OUTPUT*****
Total Addition Element of data:250
#include<stdio.h>
void data(int a[5])
{
int r=0;
for(int i=0;i<5;i++)
r=r+a[i];
printf("Total Addition Element of data:%d",r);
}
int main()
{
int x[5]={80,77 14,78,01};
data(x);
}
*****OUTPUT*****
Total Addition Element of data:250
Recursion: The process of calling a function by itself is called Recursion and the function that calls itself is called Recursive Function.
How To Find Factorial of any number in C?
#include<stdio.h>
void factorial(int n,int f)
{
if(n>=1)
{
f=f*n; n--;
factorial(n,f);
}
else
printf("Factorial:%d",f);
}
int main()
{
int num;
printf("Enter any number to find factorial:"); scanf("%d",&num);
factorial(num,1);
}
*****OUTPUT*****
Enter any number to find factorial:5
Factorial:120
#include<stdio.h>
void factorial(int n,int f)
{
if(n>=1)
{
f=f*n; n--;
factorial(n,f);
}
else
printf("Factorial:%d",f);
}
int main()
{
int num;
printf("Enter any number to find factorial:");
scanf("%d",&num);
factorial(num,1);
}
*****OUTPUT*****
Enter any number to find factorial:5
Factorial:120
0 Comments