Function is
block of statements and is call for specific task to be performed in a program.
Through function we can divide whole
program in a module and thus it will be very easy to deal with modules or small
pieces of programs. Any C program contains at least one function, that is main() function. There is no limit on
number of functions that might be present in a C program. Each function in a
program is called in the sequence specified by the function calls in main().
After performing task by each function, control returns to the main(). Function
cannot be define inside another function
Every function in C programming language should be
declared before they are used. These type of declaration are also called function prototype. Function prototype
gives compiler information about function name, type of arguments to be passed
and return type. The syntax of function prototype is as follow:
Return-type <function-name> (type(1)
argument(1),....,type(n) argument(n)); For example:
int
add(int a, int b);
In the above example, int add(int a, int b); is a function prototype which provides
following information to the compiler:
•
return type of the function is int.
•
name of the function is add()
•
two arguments of type int are passed to
function.
Function prototype is not needed if user-definition
function is written before main() function.
For function to perform specific task, it must be
called or invoked which is called as function
call. The function can be called or invoked using following syntax:
function-name
(argument(1),….argument(n));
When function is invoked using above syntax, control
of program jump from that statement to function definition and execute the code
inside that function.
Function
definition contains programming codes to perform specific task. The syntax
of function definition can be as follows:
return-type function-name
{
Block
of statements;
}
Call by value
is used when called function does not need to modify the value of the caller’s
original variable. In this case a copy of the variable is passed to the
function and when the function changes its values then it has no effect on the
value of the variable in the main function as shown below:
Output of variables a and b won’t change and
will printed as a = 10 and b = 20.
Call by
reference means that compiler will not create a local copy of the variable
which you are referencing to. It will get access to the memory where the
variable saved. When you are doing any operations with a referenced variable
you can change the value of the variable. Address is passed by using symbol “&” and is received by using
asterisk symbol “*” shown as:
Output of variables a and b changes because
it was passed by reference.
Comments
Post a Comment
if you have any confusion then ask hear.