#UserDefinedFunctions In C Language With Example 0 1886

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.

To Create A User Define Functions

You will have to keep following points in your mind :

  • Declare the function with appropriate prototype before calling and defining the function.
  • Function name should be meaningful and descriptive.
  • Keep same argument data type and return data type in Declaration and Defining the function.
  • Pass the same data type arguments which are provided in Declaration and Definition.
  • The arguments/parameters which are used while calling the functions are known as Actual Arguments/Parameters and Arguments/Parameters which are used in the definition of the function are known as Local (Format) Arguments/Parameters.

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

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;

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

{

ans = ans * n;

}

return(ans);

}

Previous ArticleNext Article
Indian Mirchi will serve you taste in Indian news, general knowledge and information. Hope you like the story I write here and expect your points of discussion to improve them and give the best chili test out of the story. I will write tutorials also that helps students. In case you are looking for any specific information then share me in the comment.

Send this to a friend