0% found this document useful (0 votes)
10 views

Function Arguments

java
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Function Arguments

java
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Function arguments:

function arguments also known as parameters are the variables


that will receive the data sent by the calling program.
there are two types of arguments:
Actual arguments:
The parameter used in function definitions are called
formal parameter
Formal arguments:
The parameter used in function calls are called actual
parameter

Passing arguments in Functions:


Call by value:
The call by value method of passing arguments to a
function copies the actual value of an argument into the
formal parameter of the function.
N.B: changes made to the parameter inside the function
have no effect on the argument

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

Difference Between Call by Value and Call by Reference:


Returning value:
A function may or may not send back any value to the
calling
Function. A function may have more than one return
statement. This situation arises when the value returned is
based on certain condition, By default, all functions return
int type data.
We can force a function to return a particular type of data
by
using a type specifier in the function header
Example:

The return statement can take one of the following forms


 return : This form of return does not return any value.
When a return is encountered, the control is
immediately
passed back to the calling program
Example:
If (error)
Return.

 return (expression): This form of return with an expression


returns the value of the expression.

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

 The function prototype must show that the argument is a


string,
for the above function definition, the prototype can be written
as
 A call to the function must have a string array name without
subscript as its actual argument. Example

You might also like