Professional Documents
Culture Documents
Parameters of a Function
Function recursions
Predefine Function in C
Complex function in C
>
When any program is long or same of code repeating may times then we try to cut the program in different parts so that program become easily understandable , easy to debug and size of code will be reduce.
>
// Function Defination
return_type Function_name ( parameters1, parameter 2, ..) { statement 1; statement 2; . // Function Body statement n; return expression; }
>
#include<stdio.h> #include<conio.h> int sum (int,int); int main() { int p; p=sum(3,4); printf(%d,sum); return 0; } int sum( int a,int b) { int s; s=a+b; return s; }
//function declaration
//function call
Function reduce the redundancy of code. It breaks down a large program in small modules to improve readability, portability as well as makes the program easy to debug. For example if you write a program to find the factorial of n number. you should write number of line of code but when you use function it reduce huge number of code into small one. function of factorial fact(int n) { int factorial; if(n==1) return 1; else factorial=fact(n-1)*n; return factorial; }
Name of function includes only alphabets, digit and underscore. First character of name of any function must be an alphabet or underscore. Name of function cannot be any keyword of c program.
Name of function can not be exactly same as of name of function in same scope.
Call by reference
Function in C with no parameters
3 Development Tool: MS Visual Studio 2005 3 Development Tool: MS Visual Studio 2005 3 Development Tool: MS Visual Studio 2005 Function in C parameters but no return type
Void world1(); Void world1(); Char c=* Int i, j; Void main() { char *msg = I know function; World1(); printf(%c ); world2(); }
Void swap(int, int) Void main() { int a=10, b =15; printf(before a = %d b= %da,b); swap(a,b); printf(after a = %d b= %da,b); } int swap(int a, int b) { }
Void operation(int, int) Void main() { int a=10, b =15, c; c=operation(a,b); swap(a,b); printf( c = %dc); } int operation() { int c; c = ++a * b++; }
Int main() { int total; total=sum(2); printf("%d",total); return 0; } int sum(int i) { static int even=0; if(i<=20){ even=even+i; sum(i+2); //calling same function } return even; }