Variable val is a global variable. So it is visible in the translation unit until it will not be hidden by a declarationn with the same name. So you could write much simpler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
double val = 100.0;
int main()
{
double x;
x = val; // assign value of val to x
cout << x << '\n';
return 0;
}
Or if you want to use a function then
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
double val = 100.0;
inlinedouble f() { return ( val ); }
int main()
{
double x;
cout << f() << '\n';
x = f(); // assign value of val to x
cout << x << '\n';
return 0;
}