User defined functions in C - C-Tutorial

Latest

Friday, 11 November 2016

User defined functions in C

User defined functions in C


  • ·         The functions which are defined by the user at the time of writing program.
  • ·         Functions are made for code reusability and for saving time and space.
  • ·         For creating user defined function user can follow the three things.

1. Function prototype
2. Calling function
3. Called function

Function prototype (or) Function Declaration:
General syntax of function declaration is,
return-type function-name (parameter-list) ;
Like variable and an array, a function must also be declared before it is called. A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration consist of 4 parts.
·         return-type : It specifies what type of values returned by the function
·         function name: It specifies the name of the function and it follows the identifiers rules.
·         parameter list: The arguments used to perform the function implementation
·         must terminating with semicolon

Calling Function:
·                  A function can be accessed by specifying it's name followed by list of arguments enclosed with parenthesis and separated by comma operator. 
·                  If  in function call doesn't require any arguments, Then empty pair of parenthesis must follow the name of the function.
·                  If return type is void in function prototype then syntax to write the calling function is as follows.                  calling_function(var1,var2,var3,......,varn);
                                                                                                Actual arguments (or) actual parameters
·                  If return type is not void in function prototype then syntax to write the calling function is as follows.                            Var_name=calling_function(var1,var2,var3,......,varn);
Ex:          1. message();
                2. add(x,y);
                3. x=add();
                4. x=rfact(n);
Called function (or) Function Definition:
Function definition contains implement code of the function.  The general format of a function definition is:

Syntax: Return_type Function_name (datatype arg1, - - - - - - - , datatype argn)
              {
                        Local Variable Declarations
                        --
                        --          (Implementation Code)
                        --
                        return value;
              }
Where,
  • arg1, arg2,  --- , argn arguments are known as “formal arguments”.  List of values passed from the function call are temporarily stored in these formal parameters.
  • Variables declared in the function definitions are called local variables.
  • After completing implementation of the function, it should return a value to the calling function with a return statement.
  • The syntax of a return statement is:
Syntax:           return value;
            If the function should not return any value, then simply placed as:

                        Syntax:           return;

No comments:

Post a Comment