Type of User defined functions:-
The user defined functions may be
classified in the following three types. Each type is based on the formal
arguments or parameters passed to the functions and the usage of return
statement.
No argument no return:-
A function is called without passing
any argument
from the calling portion of a program and also the function does not return any
value to the calling function.
With argument no return:-
A function is called and formal
arguments are passed from the calling program but function does not return any
value to the calling program.
With argument with return:-
A function is invoked with formal arguments
form the calling portion of a program and the function return value to the
calling program.
Prototype of a function:-
The prototype tells ‘C’ compiler in
advance about some characteristics of a function used in a program.
Syntax:-
type function_name(type_argument1,type_argument2,……..);
The function “type” is the type of the
returned value. If the function does not return a value, the function name is
any legal identifier followed by the parenthesis.
The arguments or parameters are given
inside the parenthesis, preceded by their type and separated by commas.
Function Definition:-
A function definition contains the
code that will be executed when a function is called upon.
Syntax:-
return_type function_name(type_argument1,type_argument2,….);
{
statement;
statement;
}
Accessing a function:-
A user defined function is called from
the main program simply by using the name of the function, including the
parenthesis followed by a semicolon.
Parameter Passing:-
Parameter passing is a method for
communication of data between the calling function and called function. This
can be done by following two type-
- Call by value
- Call by reference
Call by value:-
In C, arguments are passed by the
value, which means that a copy of the argument is passed to function. The
function can change the value of this copy but cannot change the value of the
argument is calling function.
Note:-
The argument that is passed
is often called an actual argument and while the received copy is called a
formal argument or formal parameter.
Call by reference:-
Call by reference technique passes the
addresses or references of the actual parameter to the called function. Thus
the actual and formal parameters share the some memory locations. This is
achieved by applying an address operator, that is the ‘&’ to the formal
parameters in the function definition.
Recursion:-
A Recursion function is a function
that calls itself to perform a specific operation. The process of a function
calling itself is known as recursion.
No comments:
Post a Comment