0% found this document useful (0 votes)
40 views21 pages

Centre of Development of Advance Computing (C-Dac) : User Defined Function

C user defined functions with nested functions and passing an array and return array to and from function

Uploaded by

Anurag Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views21 pages

Centre of Development of Advance Computing (C-Dac) : User Defined Function

C user defined functions with nested functions and passing an array and return array to and from function

Uploaded by

Anurag Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Centre Of Development Of

Advance Computing (CDAC)

USER DEFINED
FUNCTION

CREATED BY:
Anurag Garg
Devesh Pandey
Rajat Saini

Definition

A 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:
Example:

These are also known as Pre defined functions.


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.

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-DEFINED
FUNCTION
In order to make use of user-defined functions,
we need to establish three elements that are related
to functions.

Function declaration
Function definition
Function Call

Function declaration
Syntax:
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 <stdio.h>
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 */

Let us call the function swap() by


passing values by reference :
#include <stdio.h>
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


value
int 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 Garg

Nesting of Functions

One function using another function inside its body


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

NOTE: be careful, nesting of functions can lead to infinite nesting

Recursion

Special 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 infinite

Passing 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

You might also like