can anyone explain why this program isn't working ? and how to retify this error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can anyone explain why this program isn't working ? and how to retify this error?

#include<iostream> using namespace std; int fun(int &x) { return x; } int main() { cout << fun(10); return 0; }

22nd May 2021, 5:56 AM
Sridevi R
2 Answers
+ 5
Adding to Sarada Lakshmi suggestion, int fun( int& x ) Means function `fun` requires <x> to be a readable & writable reference to an `int` variable. And also means function `fun` *may* modify <x> argument somehow. fun( 10 ) Means to call function `fun` and pass an `int` literal (read-only data) as argument for function fun. Function `fun` expected a readable & writable `int` reference, but it received instead a literal (read-only data), so it gives a warning.
22nd May 2021, 6:25 AM
Ipang
+ 4
#include<iostream> using namespace std; int fun(int x) { return x; } int main() { cout << fun(10); return 0; } Take by value or by const reference, unless you intend to do something.
22nd May 2021, 6:13 AM
sarada lakshmi
sarada lakshmi - avatar