user defined functions in c

23
USER DEFINED FUNCTIONS IN C

Upload: harendra-thind

Post on 29-Nov-2014

378 views

Category:

Technology


6 download

DESCRIPTION

User defined / Pre-defined functions in C Function declaration, Function Call, Function definition, Types of functions, Categories of functions

TRANSCRIPT

Page 1: User defined functions in C

USER DEFINED FUNCTIONS IN C

Page 2: User defined functions in C

DefinitionA set of statements working together with

common goal is known as function.Also known as subprograms which are used

to compute a value or perform a specific task.They can’t run independently and are always

called by the main() program or by some other function.

Page 3: User defined functions in C

Categories of functions

Library functions

User-Defined functions

Page 4: User defined functions in C

Library functions

These are also known as Pre defined functions.Ex.

scanf(), printf(), getch(), strlen(), strcmp(),strcat(), sqrt(), pow() are this are library functions.

Page 5: User defined functions in C

User Defined FunctionsUser defined functions are self-contained

blocks of statements which are written by the user to compute or perform a task.

They can be called by the main program repeatedly as per the requirement.

Page 6: User defined functions in C

Uses of functionsThey are very much useful when a block of

statements has to be written/executed again and again.

They are useful when program size are too large and complex.

It works like top-down modular programming technique to solve a problem.

They are also used to reduce the difficulties during debugging a program.

Page 7: User defined functions in C

ELEMENTS OF USER-DEFINEDFUNCTIONIn order to make use of user-defined

functions, we need to establish three elements that are related to functions.

Function declarationFunction CallFunction definition

Page 8: User defined functions in C

Function declarationSyntax:

function_type function_name(arguments list);

Ex.int add(int , int);

Page 9: User defined functions in C

Function callThe program that calls the function is referred

to as the calling program or calling functions

Syntax:function_name(actual parameters);

Ex.

add(a,b);

Page 10: User defined functions in C

Function definition

The function definition is an independent program module that is specially written or implement the requirements of the function.

Page 11: User defined functions in C

main(){function1();….function2();}function1(){…}function2(){…function1();}

Page 12: User defined functions in C

Types of functions1) Function with no arguments & no return

value.2) Function with arguments & no return

value.3) Function with arguments & return one

value.4) Function with no arguments & return one

value.5) Function return multiple values.

Page 13: User defined functions in C

Function with no arguments & no return value.

void series( );main( ){series( ); /* function called */getch( );}

Page 14: User defined functions in C

Void series( ){int x , n , i , s=0;printf(“Enter the value x & n”);Scanf(“%d%d”, &x ,&n);For(i=1; i<=n; i++)s=s+pow(x,i);Printf(“Sum of series is %d”,s);}

Page 15: User defined functions in C

Function with arguments & no return valuevoid series(int , int);main( ){int x , n;printf(“”Enter the value of x & n);scanf(“%d%d”,&x&n);series(x,n); /* function call with actual

arguments */getch();}

Page 16: User defined functions in C

void series(int x , int n){int i , s=0;for(i=1;i<=n;i++);s=s+pow(x,i);printf(“Sum of series is %d”,s);}

Page 17: User defined functions in C

Functions with arguments & return one valueint series(int , int); main( ){int x , n , r;printf(“Enter x & n”);Scanf(“%d%d”,&x&n);r=series(x,n);printf(“Sum of series is %d”,r);getch( );}

Page 18: User defined functions in C

int series(int x , int n){int i , s=0;for(i=1;i<=n;i++)s=s+pow(x,i);return(s);}

Page 19: User defined functions in C

Function with no argument & no return valueint series( );main( ){int r;r=series( );printf(“Sum of series %d”,r);getch( );}

Page 20: User defined functions in C

int series( ){int x , n , i , s=0;printf(“Enter x & n”);scanf(“%d%d”,&x,&n);for(i=1;i<=n;i++)s=s+pow(x,i);return(s);}

Page 21: User defined functions in C

Function return multiple values int check( );main( ){int r;r=check( );if(r==1)pintf(“Even number”);elseprintf(“Odd number”);getch( );}

Page 22: User defined functions in C

int check( ){int n;printf(“Enter a number”);scanf(“%d”,&n);if(n%2==0);return(1);elsereturn(0);}

Page 23: User defined functions in C

Thank you