HitBrother

#UserDefinedFunctions In C Language With Example

user-defined-functions

User Defined Functions

Functions defined by us are known as User Defined Functions.

User Define Functions are created to perform some specific task by the programmer, for example if you want to find the sum of all array elements using your own function, then you will have to define a function which will take array elements as an argument(s) and returns the sum of all elements.

The logic/code will be written in the defined function and you have to just call this function within the program by passing actual arguments.

Definition

A function is a block of code that performs a specific task. C allows you to define functions according to your need. These functions are known as user-defined functions.

Advertisement

To Create A User Define Functions

You will have to keep following points in your mind :

main( ) function is also user defined function because the definition of main( ) is defined by us.

'); var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://ad.admitad.com/shuffle/289c251618/'+subid_block+'?inject_to='+injectTo; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); })();

Example

Q. Write a program to calculate the power of any number?

int power(int, int);

main( )

{


int n, p, ans;

printf(“Enter number and its power”);


scanf(“%d%d”, &n,&p);

ans = power(n,p);

printf(“%d”,ans);

getch( );

}

int power(int n, int p)

{

int ans=1, i;

'); var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://ad.admitad.com/shuffle/289c251618/'+subid_block+'?inject_to='+injectTo; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); })();

for(i=1;i<=p;i++)

{

ans = ans * n;

}

return(ans);

}

Advertisement