14.6 – Functions – pass by address

by subbu on January 19, 2014

Arguments – pass by address

When we send arguments to a function, only the values of actual arguments are assigning to the formal arguments called pass by value. The change in formal arguments would not make any change in actual arguments.
There are some cases where, actual arguments need to be changed through the formal arguments from the called by function.

Traditionally a function may accept any number of arguments but, capable to return a single value.
There are some cases where, more than one value need to be returned from a function.

Answer to these requirements is pass by address

Here we send the addresses of actual arguments as arguments; formal arguments would be pointers to the actual arguments. By dereferencing with formal arguments, we can access and modify the actual arguments.

pass by address

process(&x);   /* sending address of x to process */

Here x is the local variable of main(), its address is sending as argument to the function process
Now the formal argument of process must accept the address of x, hence it must be a pointer to x.

void process(short *p)  /* p is the pointer to x */
{
*p=*p+100;
}

By dereferencing operator value at * , we can modify the value of x through the pointer p

*p=*p+100;

Program

#include<stdio.h>
void process(short*);
int main()
{
int x=243;
process(&x);           /* sending address of x */
printf("\nx=%d",x);    /* printing the value of x */
return 0;
}
void process(short *p)  /* pointer to x         */
{
*p=*p+100;          /* modifying the value of x by dereferencing*/
}

Output:
x=343

The main advantage of passing pointers (addresses) as arguments is, called by function can access and modify the objects of calling function.

Specification: Accept two numbers into two variables and print their values by interchanging them using a sub function called swap()

addresses as arguments

x, y are the variables belongs main(), the addresses of which are sending into the function swap() as actual arguments

swap(&x,&y);

p and q are the formal arguments of function swap, stores the addresses of actual arguments x and y. Now p and q are the pointers to x and y.

void swap(int *p,int *q)  /* pointers to actual arguments */
{
int temp;
temp=*p;                     /* interchanging */
*p=*q;
*q=temp;
}

Now by dereferencing, both the pointers p and q could access and swap the values of x and y from swap function

#include<stdio.h>
void swap(int*,int*);
void main()
{
int x,y;
printf("Enter an integer into x:");
scanf("%d",&x);
printf("Enter an integer into y:");
scanf("%d",&y);
swap(&x,&y);
printf("Value of x: %d",x);
printf("\nValue of y: %d",y);
return 0;
}
void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}

Execution:
Enter an integer into x:34
Enter an integer into y:56
Value of x: 56
Value of y: 34

Specification: Accept two sides of rectangle and print the area, perimeter

returning multiple values

The purpose of rectange() function is to accept the length, breadth from the main() and return area, perimeter back to main()

l, b, area and peri are the variables belongs to main(), values of l and b, addresses of area and peri are send as arguments to the function rectangle().

rectangle(l,b,&area,&peri);

Here the values of l, b (length, breadth) are assigned to the formal arguments x,y and formal arguments a, p stores the addresses of both area, peri. Now a, p are the pointers to the actual arguments area, peri.

void rectangle(int x,int y,int *a,int *p)
{
*a=x*y;                                            /* Assigning the value to area */
*p=2*(x+y);                                     /* Assigning the value to peri */
}

From the rectangle() function using value at (*) operator we can indirectly access area, peri as *a (value at a) and *p (value at p)

#include<stdio.h>
void rectangle(int,int,int*,int*);
void main()
{
int l,b,area,peri;
clrscr();
printf("Enter two sides of rectangle:\n");
scanf("%d%d",&l,&b);
rectangle(l,b,&area,&peri);
printf("Area %d",area);
printf("\nPerimeter %d",peri);
getch();
}
void rectangle(int x,int y,int *a,int *p)
{
*a=x*y;
*p=2*(x+y);
}

Execution:
Enter two sides of rectangle:
12
15
Area 180
Perimeter 54

Advantages with pass by address

  • It allows to change the actual arguments with a function. Some times it is needed to do so.
  • It helps to return multiple values from a function, otherwise only possible to return a single value from a function

Disadvantages with pass by address

  • Here actual arguments must be variables but, not constants and expressions because we can’t send the addresses of constants and expressions.
  • Dereferencing through pointer is slower than accessing directly

Previous post:

Next post: