Functions in C
Functions in C
Top-down design
a. Call by value
In this method, the value of the actual
arguments in the calling function is copied
into corresponding formal arguments of the
called function.
Therefore, the changes made to the formal
arguments have no effect on the values of
actual arguments
Advantages
It protects the original data in the calling
function, since the changes inside the called
function will not be reflected over it.
It allows even expressions as actual arguments
rather than a single variable.
Disadvantage
1)It is a one- way transfer of information from
the calling function to called function.
Information cannot be transferred back
through arguments.
Call by reference / call by address:
In this method, the addresses of the actual
arguments are copied into the formal arguments.
Any reference to a formal parameter is same as a
reference to the actual parameter in the calling
function.
Thus, when the called function changes a value,
it actually changes the variable in the calling
function.
To pass an address, the address operator (&) is
used. A variable that holds this address is known
as pointer variable. It is represented using the
dereferencing operator (*).
Recursion
Recursion is a process in which a function calls
itself repeatedly till some specified condition is
satisfied.
Example
Example
#include <stdio.h>
void display(int,float);
void main() {
struct account
{
int accno;
float bal;
};
struct account a = {100,25000};
display(a.accno, a.bal); }
void display(int no, float balance) {
printf(“Account no :%d Balance : %f ”, no,
balance); }
b. Passing the entire structure
When a structure is passed to a function, the
transfer is by value. i.e., the values will be copied
in a local structure and if any of the structure
variables are altered inside the function, the
changes will not be recognized in the calling
function. If we need the changes, the entire
structure has to be returned from the function.
Also we have to specify the type of the formal
parameters and return type.
#include <stdio.h>
void display (struct distance);
struct distance sum(struct distance, struct
distance);
void main( ) {
struct distance
{
int feet;
int inch;
}d1, d2, d3;
printf(“Enter 2 distances: feet & inch \n”);
scanf(“%d %d”,&d1.feet, d1.inch);
scanf(“%d %d”,&d2.feet, d2.inch);
printf(“Distance l is”)
display(d1); /* pass the structure
variable d1 */
printf(“Distance 2 is”);
display(d2); /* pass the structure
variable d2 */
d3 = sum(d1,d2);
printf(“Sum is \n”);
display(d3);
}
void display(struct distance d)
{
printf(“Feet : %d Inches %d \n”, d.feet, d.inch);
}
struct distance sum(struct distance dist1, struct
distance dist2)
{
struct distance dist3;
dist3.feet = dist1.feet + dist2.feet;
dist3.inch = dist1.inch + dist2.inch;
return dist3;
}
c. Passing the address of a structure
The address of a structure variable or member can
be passed as arguments using pointers. A structure
passed in this manner is said to be passed by
reference. Hence, if any of the structure members
are altered within the function, the changes will be
recognized in the calling function. Using the
pointer to the structure, the members of the
structure can be accessed with ->operator
#include <stdio.h>
struct distance
{
int feet;
int inch;
};
void increment(struct distance *);
void main( )
{
struct distance d = {10,1};
printf(“Before increment : %d %d”, d.feet, d.inch);
increment (&d);
printf(“After increment :%d %d”, d.feet, d.inch);
}
void increment(struct distance *dist)
{
(dist->feet)++;
(dist->inch)++;
}
Self-referential structures
If a member of a structure is a pointer to itself,
then it is said to be self-referential structures. Self
referential structures are very useful in
applications that involve linked data structures.
The general form is
struct tag
{
member 1;
member 2;
…………
struct tag *ptrname; /* pointer to itself */
};
where