Pointers and Functions in C
Pointers are often moved to function arguments. This allows the data elements in the calling section of the function to be accessed through the function,converted into functions,and then returned to the program's calling section in modified form. We specify this use of pointers to argue by reference or address or location.
Let we discuss about call any function by passing arguments.
When an argument passes the value, the data is copied to the item function. Therefore, any change in the data component in the function is not considered in the calling function. However, when an argument is passed by a reference (i.e. when a pointer is passed to a function), the address function of the data item is given. The contents of this address can be freely accessed between the function or the calling function. Also, any changes to the data item will be recognized in both functions. Therefore, using a pointer as a function argument allows the relevant data item to be converted entirely from within the function.
Let we discuss when pointer are used as a argument to function.
When pointers are used as an argument for a function,some caution is required with declaring a formal argument within the function. A typical pointer argument followed by an asterisk (*).The name of the variable is not included, each pointer must have an asterisk after the data type of the argument.
In C language a function can be called by the calling program in two ways:
- Call By Value
- Call By Reference
Call By Value
When the calling function calls a function, the values of the function are provided by the calling program. These values can be used in functions. The calling function does not accept any change in value between functions, but the change is available locally in the function. This method is known as the value calling function.
Let we understand with the help of example:
Example 1:Program to add any number.
#include <stdio.h>
#include <conio.h>
void main()
{
int x=10,y=20;
void add(int,int);
printf("Value before function call x=%d y=%d\n",x,y);
add(x,y);
printf("Value after function call x=%d y=%d\n",x,y);
getch();
}
void add(int a,int b)
{
a=a+10;
b=b+10;
printf("Value of x and y inside the function x=%d y=%d\n",a,b);
}
Out Put:
Value before function call x=10 y=20
Value of x and y inside the function x=20 y=30
Value after function call x=10 y=20
Example 2:Program to swap numbers.
#include <stdio.h>
#include <conio.h>
void swap (int,int);
void main ( )
{
int x , y ;
x = 10 ;
y = 20 ;
printf("Value of x and y before calling x=%d y=%d\n",x,y);
swap ( x , y ) ;
printf("Value of x and y after calling x=%d y=%d\n",x,y);
}
void swap (int a,int b)
{
int temp ;
temp=a ;
a=b ;
b=temp ;
printf("Value of x and y inside function x=%d y=%d\n",a,b);
}
Out Put:
Value of x and y before calling x=10 y=20
Value of x and y inside function x=20 y=10
Value of x and y after calling x=10 y=20
Call By Reference
A function can be interpreted with pointers as its argument.Such a function is called by the calling function with the address of a variable as its argument.The address of the variable for the pointers is changed and when its value is changed in the function,than it is automatically located.The change is made indirectly and is accepted by the calling function.This method is known as call by reference.
Let we understand with the help of example:
Example 1:Program to add any number.
#include <stdio.h>
#include <conio.h>
void main()
{
int x=10,y=20;
void add(int*,int*);
printf("Value before function call x=%d y=%d\n",x,y);
add(&x,&y);
printf("Value after function call x=%d y=%d\n",x,y);
getch();
}
void add(int *a,int *b)
{
*a=*a+10;
*b=*b+10;
printf("Value of x and y inside the function x=%d y=%d\n",*a,*b);
}
Out Put:
Value before function call x=10 y=20
Value of x and y inside the function x=20 y=30
Value after function call x=20 y=30
Value of x and y inside the function x=20 y=30
Value after function call x=20 y=30
Example 2:Program to swap numbers.
#include <stdio.h>
#include <conio.h>
void swap (int*,int*);
void main ( )
{
int x , y ;
x = 10 ;
y = 20 ;
printf("Value of x and y before calling x=%d y=%d\n",x,y);
swap (&x,&y) ;
printf("Value of x and y after calling x=%d y=%d\n",x,y);
}
void swap(int *a,int *b)
{
int temp ;
temp=*a ;
*a=*b ;
*b=temp ;
printf("Value of x and y inside function x=%d y=%d\n",*a,*b);
}
Out Put:
Value of x and y before calling x=10 y=20
Value of x and y inside function x=20 y=10
Value of x and y after calling x=20 y=10
In this way from the above examples it is clear that how call by value and call by function works.We can say that in call by value actual arguments never changes but In call by reference actual argument can be change by changes in formal arguments.
Know more about pointers
- Pointers and Functions
- Pointers and Arrays
- Pointers to Pointers
- Command line Arguments
- Dynamic Memory Allocation
No comments:
Post a Comment