What are the elements of user defined functions?

Author: Tanmay Chakrabarty

First Published On: Monday, April 13, 2015 at 1:17 PM
About:

User defined functions

Functions are blocks of codes to perform specific tasks and return the result. However it is not mandatory for a function return anything and also a function is not limited to performing one task only. User defined functions are basic building blocks of a program and can be found in the basic structure of C program.

Functions can be classified into two categories, namely, library functions and user-defined functions. The functions which are developed by user at the time of writing a program are called user defined functions. Thus, user defined functions are functions developed by user.


Example: 

Here addNumbers() is an user defined function.

//declaring an user defined function which has been defined later
float addNumbers(float a, float b);

int main(){
     float result;
     
     /* calling user defined function from the main function */
     result = addNumbers(.5, .8);

     return 0;
     }

//defining an user defined function which has been declared earlier.
float addNumbers(float a, float b){
     return (a+b);
     }

Elements of User Defined Functions

In order to make use of a user defined function, we need to establish three elements of user defined functions which are as follows
  1. Function Declaration
  2. Function Definition
  3. Function call

Function Declaration

Function declaration is simply declaring the name of the function, the arguments and their types and the return type of the function. User need to declare a function prior to the definition of the main() function when the definition of the function is written after the definition of the main() function. If the user writes the definition of the function prior to the definition of the main() function then the need not to declare the function explicitly.

//declaring an user defined function which has been defined later
float addNumbers(float a, float b);

Function Definition

Function definition includes the parts of the function declaration along with the body or code-block for the function. User can define a function before or after the main() function.

//defining an user defined function which has been declared earlier.
float addNumbers(float a, float b){
     return (a+b);
     }

Function Call

Calling an user defined function is similar to the calling of library functions, write the name of the function and provide the arguments. If you need to store the returned data in a variable, then assign this call to a variable.

int main(){
     float result;
     
     /* calling user defined function from the main function */
     result = addNumbers(.5, .8);

     return 0;
     }

Read Next: What are the necessities or advantages of user defined functions?

Recommended

Comments

Contact Us

Author