User Defined Functions

User defined functions :

A User defined function is the one defined by user for some special purpose.

Parts of function:

    • Return Type:It specifies what type value does the funtion returns. A function block. return a value. It can be void, int, char, float etc.
      One function can return only a single value.
    • Parameters:These are the arguments passed in the function.
        1. Actual parameters: These are the ones which are passed during function call
        2. Formal parameters:These are the ones which are used in function definition
    • FunctionName:lt is the name of the function. It can be any valid identifier
    • Function_Body: It contains all the statements to be executed by a function

Function Declaration :

A function must be declared before using it. It tells the compiler what type of value it returns and type and number of arguments it takes.

Function declaration is necessary when the function is not defined before its use i.e. function definition is written after the function in which it is called.

Syntax:

return_type func_name(data_type of var1,data_type of var 2,…);

Function Definition :

A function definition is that part, where the body of function is defined. It consists of all the statements that are to be executed by a function.

Syntax:

return_type func_name(parameter1, parameter 2,….)

{

…….

..//body of function

……

}

Function Call :

A function call statement is the one which invokes the function and gives the control of program from main to the called function.

A function can be called in two ways:

1. Call by reference

2. Call by value

e.g.

  • A function can also return a pointer but the variable should be made static as c doesn’t allow the address of local variable to be by function because local variables are destroyed after we come out of that block.
  • By returning pointer, we can also return an array from a function. The array can’t be returned as whole from a function.

e.g.

Returning pointer from a function

Returning array from a pointer

Leave a Reply

Your email address will not be published. Required fields are marked *