You are on page 1of 19

Definition of Function in C

Why we should use Function?

Function naming Rule.


Return Type of Function.

Parameters of a Function
Function recursions

Predefine Function in C
Complex function in C
>

Function is block or part of program.

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 Declaration return_type Function_name ( parameters1, parameter 2, ..)

// 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 definition //function body //function returning a value

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.

Name of function is case sensitive.

Return type of function. Returning pointer to user defined data type

Returning Pointer to Pointer.


Returning pointer to derived data type. Returning Pointer to array Returning Pointer to enum Returning Pointer to union Returning Pointer to structure.

Function parameters Call by values

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

Function in C with parameters and return type

#include<stdio.h> Int main() { int a=5, b=10; swap(a,b); printf(%d %d,a,b); }


3 Development Tool: MS Visual Studio 2005 3 Development Tool: MS Void swap(int a, int b) { Visual Studio 2005 3 Development Tool: MS Visual Studio 2005 int temp; temp = a; a = b; b = temp; printf(%d %d,a,b);

#include<stdio.h> Int main() { int a=5, b=10; swap(&a,&b); printf(%d %d,a,b); }


3 Development Tool: MS Visual Studio 2005 3 Development Tool: MS Void swap(int *a, int *b) Visual Studio 2005 3 Development Tool: MS Visual Studio 2005

{ int *temp; temp = *a; *a = *b; *b = *temp; }

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; }

You might also like