+ All Categories
Home > Documents > Userdefinedfunctions

Userdefinedfunctions

Date post: 06-Mar-2016
Category:
Upload: anurag-garg
View: 224 times
Download: 0 times
Share this document with a friend
Description:
C user defined functions with nested functions and passing an array and return array to and from function

of 21

Transcript

USER DEFINED FUNCTIONS

USER DEFINED FUNCTIONCREATED BY:Anurag GargDevesh PandeyRajat SainiCentre Of Development Of Advance Computing (C-DAC)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. Categories of functions Library functions: These are also known as Pre defined functions.Example: strcat(), sqrt(), pow() User-Defined functions : They can be called by the main program repeatedly as per the requirement. User defined functions are self-contained blocks of statements which are written by the user to compute or perform a task.

3 Uses of functions : These are some special uses of function -:

They 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.ELEMENTS OF USER-DEFINEDFUNCTION In order to make use of user-defined functions, we need to establish three elements that are related to functions.

Function declarationFunction definitionFunction Call

Function declarationSyntax:

function_type function_name(arguments list);

Example : int add(int , int); Function definition

The function definition is an independent program module that is specially written or implement the requirements of the function. main() { function1(); . function2(); } function1() { .. } function2() { function1(); }

Next part by Rajat Saini Function call The program that calls the function is referred to as the calling program or calling functions

Syntax: function_name(actual parameters);Example : add(a,b); While calling a function, there are two ways that arguments can be passed to a function: Function call by value : The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. Example : /* function definition to swap the value */ void swap(int x,int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; }

Now, let us call the function swap() by passing actual values :#include void swap(int x, int y); int main () { int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); swap(a,b); /* calling a function to swap the values */ printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; }

Function call by reference : The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. Example :/* function definition to swap the values */ void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put x into y */ return;}12 Let us call the function swap() by passing values by reference : #include void swap(int *x, int *y); int main () { int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); swap(a,b); /* calling a function to swap the values. */ printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; }Function with no arguments & no return value.

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

Functions with arguments & return 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( );}Next part by Anurag GargNesting of FunctionsOne function using another function inside its body

function1 ( ){function2 ( );//statements}

NOTE: be careful, nesting of functions can lead to infinite nestingRecursionSpecial case of nesting functions, where a function calls itself inside it

function1 ( ){function1 ( );//statements}

NOTE: certain condition needed to break out of the recursion, otherwise recursion is infinitePassing Array to Functions We can either have an array in parameter function (array-name , size ); // calling function with array as argument void function (int array-name[ ] , int size ) { //statements}

Or, pointer in the parameter list, to hold base address of array.function (pointer ); // calling function with array as argument void function (int *pointer ) { //statements}

Returning Array from Functions We can either have an extra array in parameter function (array1 ,array2 , size ); // calling function with two array as argument void function (int array1[ ] ,int array2[ ], int size ) { //statements}

Or, pointer as a return type , to hold base address of array.pointer1=function (pointer2 ); void function (int *pointer ) { //statements}

Thank you