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; }
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.
+ 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.