0% found this document useful (0 votes)
3 views19 pages

Call_by_value_Pointers_explaination

Uploaded by

nareshm3225
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views19 pages

Call_by_value_Pointers_explaination

Uploaded by

nareshm3225
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

FUNCTION

CALL BY VALUE
&
CALL BY REFERENCE
POINTER

Consider the declaration,


int i = 3 ;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the
integer value.
(b) Associate the name i with this memory
location.
(c) Store the value 3 at this location.
We may represent i’s location in memory by
the memory map shown in Fig
To print Address
# include <stdio.h>
int main( ) Output:
{
Address of i = 65524
int i = 3 ; Value of i = 3
printf ( “ %u\n", &i ) ;
printf ( "%d\n", i ) ;
return 0 ;
}

• ‘&’ used in this statement is C’s ‘address of’ operator


• The other pointer operator available in C is ‘*’, called ‘value at address’ operator.
• It gives the value stored at a particular address. The ‘value at address’ operator is
also called ‘indirection’ operator.

# include <stdio.h>
int main( )
{ Output
int i = 3 ;
Address of i = 65524
printf ( " %u\n", &i ) ; Value of i = 3
printf ( " %d\n", i ) ; Value of i = 3
printf ( "%d\n", *( &i ) ) ;
return 0 ;
}
• Note that printing the value of *( &i ) is same as printing the value of i.
• The expression &i gives the address of the variable i. This address can be collected in a
variable, by saying,
j = &i ;
• But remember that j is not an ordinary variable like any other integer variable. It is a
variable that contains the address of other variable (i in this case).
• Since j is a variable, the compiler must provide it space in the memory. Once again, the
memory map shown in Figure would illustrate the contents of i and j.
# include <stdio.h>
int main( ) Output
{
Address of i = 65524
int i = 3 ; Address of i = 65524
int *j ; Address of j = 65522
j = &i ; Value of j = 65524
Value of i = 3
printf ( "%u\n", &i ) ; Value of i = 3
printf ( "%u\n", j ) ; Value of i = 3
printf ( "%u\n", &j ) ;
printf ( "%u\n", j ) ;
printf ( "%d\n", i ) ;
printf ( "%d\n", *( &i ) ) ;
printf ( "%d\n", *j ) ;
return 0 ;
}
int *alpha ;
char *ch ;
float *s ;

***The declaration float *s does not mean that s is going to contain a floating-point value.
• Pointer, we know is a variable that contains address of another variable.
• Now this variable itself might be another pointer.
• Thus, we now have a pointer that contains another pointer’s address.
• k = &j and j = &i (So printing value of ‘i’ in terms of k will be)

*j=i
*k=j
i=**k
# include <stdio.h> Address of i = 65524
int main( ) Address of i = 65524
{ Address of i = 65524
Address of j = 65522
int i = 3, *j, **k ;
Address of j = 65522
j = &i ; Address of k = 65520
k = &j ; Value of j = 65524
printf ( "%u\n", &i ) ;
Value of k = 65522
Value of i = 3
printf ( "%u\n ", j ) ; Value of i = 3
printf ( "%u\n ", *k ) ; Value of i = 3
printf ( "%u\n ", &j ) ; Value of i = 3
printf ( "%u\n ", k ) ;
printf ( "%u\n ", &k ) ;
printf ( "%u\n ", j ) ;
printf ( "%u\n ", k ) ;
printf ( "%d\n ", i ) ;
printf ( "%d\n ", * ( &i ) ) ;
printf ( " %d\n ", *j ) ;
printf ( "%d\n ", **k ) ;
• The two types of function calls—call by value and call by reference.

• Arguments can generally be passed to functions in one of the two


ways:
(a) sending the values of the arguments: (call by value)
• In the first method, the ‘value’ of each of the actual arguments in the calling
function is copied into corresponding formal arguments of the called
function.
• The changes made to the formal arguments in the called function have no
effect on the values of actual arguments in the calling function
(b) sending the addresses of the arguments: (call by reference)
• The addresses of actual arguments in the calling function are copied
into the formal arguments of the called function.
• Using these addresses, we would have an access to the actual
arguments and hence we would be able to manipulate them
CALL BY VALUE
# include <stdio.h>
void swapv ( int x, int y ) ;
Output
int main( )
{ x = 20 y = 10
int a = 10, b = 20 ; a = 10 b = 20
swapv ( a, b ) ;
printf ( "a = %d b = %d\n", a, b ) ;
return 0 ;
}
void swapv ( int x, int y )
{
int temp ;
temp = x ;
x=y;
y = temp ;
printf ( "x = %d y = %d\n", x, y ) ;
}
CALL BY REFERENCE
# include <stdio.h>
void swapr ( int *, int * ) ; Output
int main( )
a = 20 b = 10
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;
printf ( "a = %d b = %d\n", a, b ) ;
return 0 ;
}
void swapr ( int *x, int *y )
{
int temp ;
temp = *x ;
*x = *y ;
*y = temp ;
}
To Study and Demonstrate Function

1) Write a C program to create user defined function to reverse string and


concatenate.
2) Write a C program to create user defined function to compute surface area and
volume of a right circular cylinder. Do not print the output in function body. Do
not use global variable. ( Use Call by Reference)
Right circular SA and Volume
# include <stdio.h>
void rightcircular( int,int,float *,float *) ;
int main( )
{ rightcircular ( int r, int h, float *a, float *v )
int radius, height ; {
float sa, vol ; *a = (2*3.14 * r * r) + (2*3.14*r*h) ;
printf ( "Enter radius of a circle " ) ; *v = 3.14 * r*r*h ;
}
scanf ( "%d", &radius ) ;
printf ( "Enter radius of a circle " ) ;
scanf ( "%d", &height ) ;
rightcircular ( radius,height,&sa,&vol ) ;
printf ( "Surface Area = %f\n", sa ) ;
printf ( "Volume = %f\n", vol ) ;
return 0 ;
}
Area and circumference of Circle
# include <stdio.h>
Void cir( int,float *,float *) ;
int main( )
void cir ( int r, float *a, float *c )
{
{
int radius ; *a = (3.14 * r * r);
float ca, circum ; *c = (2*3.14 * r);
}
printf ( "Enter radius of a circle " ) ;
scanf ( "%d", &radius ) ;
cir ( radius,&ca,&circum ) ;
printf ( “Circle Area = %f\n", ca ) ;
printf ( “Circumference = %f\n", circum ) ;
return 0 ;
}
The basic process is as follows:
•Take the destination string
•Find the NULL character
•Copy the source string beginning with the NULL character of destination string
•Append a NULL character to the destination string once copied
#include<stdio.h> void stringrev1(char str1[20])
{
/* Function Prototype*/ int i=0,len=0;
void stringrev1(char str1[20]); char temp;
/* Main Function */ while(str1[i]!='\0')
int main()
{
{
len++;
char str1[20];
i++;
int i, len=0;
}
printf("Enter first string:\n"); for(i=0;i<len/2;i++)
//gets(str1); {
scanf(“%s”,str1) temp = str1[i];
stringrev1(str1); str1[i] = str1[len-1-i];
printf("REVERSE STRING IS: %s", str1); str1[len-1-i] = temp;
}
return 0;
} }
#include<stdio.h>
void mystrcat(char str1[40], char str2[40])
/* Function Prototype*/
{
void mystrcat(char str1[40], char int i, len=0;
str2[40]);
/* Calculating length of first string */
/* Main Function */
for(i=0;str1[i]!='\0';i++)
int main() {
{ len++;
char str1[50], str2[50]; }
int i, len=0; /* Concatenating second string to first string */
printf("Enter first string:\n"); for(i=0;str2[i]!='\0';i++)
gets(str1); {
printf("Enter second string:\n");
str1[len+i] = str2[i];
}
gets(str2);
str1[len+i]='\0';
mystrcat(str1, str2);
}
printf("Concatenated string is: %s", str1);
return 0;
}

You might also like