Computer Science, asked by amanray35, 1 year ago

explain call by value and call by address using suitable program.

Attachments:

Answers

Answered by ankitsharma26
0
Explain call by value and call by address
Function call by reference in C. The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

Vinayakmittal: hlo
Answered by MacintoshTavish
0
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. In this case, changes made to the parameter inside the function have no effect on the argument.

By default, C++ uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function.

void swap(int x, int y) { int temp;  temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put x into y */ return;}

Call by reference

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

To pass the value by reference, argument reference is passed to the functions just like any other value.

void swap(int &x, int &y) { int temp; temp = x; /* save the value at address x */ x = y; /* put y into x */ y = temp; /* put x into y */ return;}

Call by pointer or Call by address

The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

To pass the value by pointer, argument pointers are passed to the functions just like any other value.

void swap(int *x, int *y) { int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put x into y */ return;}

Thank you.

Attachments:
Similar questions