Function Arguments
Function Arguments
Call by reference:
In call by reference address of the value is passed in the
function, so actual and formal arguments shares the same
address space.
N.B: value changed inside the function, is reflected inside
as
well as outside the function
Returning Pointers:
We can pass pointers to
the function as well as return pointer
from a function.
N.B: It is not recommended to return the address of a local
variable outside the function as it goes out of scope after
function returns.
The main reason behind this scenario is that compiler always
make a stack for a function call.
As soon as the function exits the function stack also gets
removed which causes the local variables of functions goes out
of scope.
OVERCAME:
It can overcome by using static variable. Static Variables has a
property of preserving their value even after they are out of
their scope.So to execute the concept of returning a pointer
from function in C you must define the local variable as a static
variable
Recursion:
Recursion is the process of repeating items in a self-similar
Way. A function that calls itself is known as a recursive
function. And, this technique is known as recursion.
N.B: The recursion continues until some condition is met to
prevent it. To prevent infinite recursion, if...else statement (or
similar approach) can be used where one branch makes the
recursive call,
and other doesn't
Characteristics
A recursive function is a function which calls itself
The speed of a recursive program is slower because of stack
overheads
A recursive function must have terminating conditions, and
recursive expressions
Advantages
It requires few variables which make program clean
It shorten the complex and nested code
Disadvantages
It is hard to debug recursive function
It is tough to understand the logic of a recursive function
Passing arrays to functions:
Like the values of simple variables, it is also possible to pass
the values of an array to a function
Pass one dimensional array:
To pass a one dimensional an array to a called function, it is
sufficient to list the name of the array, without any subscripts,
and
the size of the array as arguments.
Pass multi dimensional array: To pass multi dimensional an
array to a called function, The size of the second dimension
must be specified.
Rules to pass an array to a function
The functions must be called by passing only the array name
In the function definition, must be an array type; the size of
array
does not need to be specified. For two dimension array The size
of the second dimension must be specified
The function prototype must show that the argument is an
array, For two dimension array we must indicate that the array
has two
dimensions by including two sets of bracket.
Passing string to function:
As strings are character arrays, so we can pass strings to
function in a same way we pass an array to a function.
Basic rules are:
The string to be passed must be declared as a formal
argument of
the function when it is defined