+1

what mean of int &x in this program

#include <iostream> using namespace std; int func(int &x,int y=10) { if(x%y==0) return x++; else return y--; } main() { int p=20,q=23; q=func(p,q); cout<<p<<" "<<q<<endl; p=func(q); cout<<p<<" "<<q<<endl; q=func(p); cout<<p<<" "<<q<<endl; return 0; }

c++

2/9/2020 6:33:56 AM

abdullah

10 Answers

New Answer

+1

When call func1(p, q) it similar x=p and y=q then the function work on x and y without changing p and q in main() function. When call func2(p, q) it similar that only y=q and function work on p and y any change happens to y does not change q but p are used than any change happens inside the function will happen in main(). Look again: https://code.sololearn.com/c9tGz3Z0DUKN/?ref=app

+3

it Passes the value by reference, by using reference the function can modify actual value in the main function, try by removing the reference to check

+1

Every variable is a memory location, which has its address defined. That address can be accessed using the ampersand (&) operator (also called the address-of operator), which denotes an address in memory. Address-of : operator (&): returns the memory address of its operand. &x it mean : the address in memory that hold the value of variable x of type integer. When passing by value in a function (use x), it does not change the value of the variable, but rather makes a copy of the value and does its job. As for passing by reference (use &x), it does not make a copy of the variable, but rather uses the variable itself, and any change inside the function will change its value.

+1

x%y it mean just the reminder of the division x/y. in this case it return x-- when the reminder == 0. and return y++ when the reminder != 0. When you call the function func(p, q) you passing by reference p, and passing by value q. The function store the value of q in new adress (adress of y) any changes happens to y does not affect p, but it store the value of p in the same adress (adress of x same adress of q), and any changes happend to x affect q.

+1

thank you!😍

0

it mean in x%y the value of x is its adrress

0

0

See this code you will see the changes. https://code.sololearn.com/c9tGz3Z0DUKN/?ref=app you will see when we call func1 (pass by value) that x and y take the value of p and q then the value of x and y change without changing rhe value of p and q. in the second case you will see when we call func2 (pass by reference) that x take the value of p then the value of x change and even value of p change too.

0

but in the case when we call fun2 we give q value not y

0

then what is the value of y?