Your Only Destination To Empower Your Computer Programing Knowledge. Sky is not so high, You can Touch it if You Try

Function

Hello friends,
When there is much load on us then what we do?Oviously we divide load into parts and reduce the complexity of that work or load.Same is the case with functions.In this article i will discuss secret about function in c programming.
FUNCTION:-
Function is a self contained block of statements which perform a predefined task.This definition is given by every programmer,what it mean?I simple language we can say that function is a set of statements which solve small part of a complex problem.when there is a complex task then by dividing that task into different functions which execute separately from other function,we can perform that task perfectly with less error.
Function are mainly of two types:-
1.USER DEFINED FUNCTIONS
2.BUILT IN FUNCTIONS
1.USER DEFINED FUNCTIONS:-
User defined functions are the type of functions which is defined by user for their convenience as per requirement.
Ex .function to find out factorial of a number.
2.BUILT IN FUNCTIONS:-
Built in function is a type of function in which function is predefined in header files,user can use it by just passing parameters.For using these functions particular header file should includes in program otherwise error shown.
Ex. Main is also a built in function.
SYNTEX OF FUNCTION:-
Return type function_name(arguments passed)
{
Statements.......
...........
}
Function should be declare in main function then we can defined it.
Function defined outside the main.Two types of parameters passed in function.
1.Actual parameters:-These are the arguments in which value of result actually shown to user.
2.Formal parameters:-Formal parameters are the parameters passed during function definition which replaced by actual parametes in calling function in main function.
Ex of function:-
A simple program to find factorial of a number through function
#include"stdio.h"
#include"conio.h"
Void main()
{
int fact;
//function declaration
fact=factorial(int a);
printf("factorial of a number is=%d",fact);
Getch();
}
Int factorial(int n)
{
int f=1,i;
printf("Enter the number to find factorial");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
Return(f);
}
Output:-
Enter the number to find factorial
5
Factorial of a number is=120
Ok friends,this is the concept of function in c language.
I hope you learn if any mistake done by me then plz suggest.
Thanks.












No comments:

Post a Comment