-2

i have just started ad my study and has been introduced to c++, just a week ago, and i struck something that irritates me: i have a base code like this

double a,b,c;

cout<<"please add in your values"<<endl;;
cout<<"a :";cin>>a;
cout<<"b :";cin>>b;
cout<<"c :";cin>>c;cout<<"\n";

dis(a,b,c);

return 0;

now i have also created a function (by the name dis) which look like this

void dis(double a,double b, double c){
double d;
double* p=&d;

d=(pow(b,2))-(4)*(a*c);
cout<<"result: "<<d;

cout<<*p;}

now i do know how to implement my result - but the way it has to be done gets on my nerves - so i wanted to create a pointer so that i can snatch up the address of the value and create a new int which is equal to the value stored on the address

Now comes my question
if i know an address for any value then how can i refer to it form my "main" so that i can create an int that contains the answer instead of doing like it is normaly done ?

I hope you know what i mean :D

Holycrabbe
  • 63
  • 1
  • 1
  • 11
  • Your current code `passes by value` all the input stuff. You would want to `pass by reference` a forth variable which would behave as the output of your function. Learn more here: [**The difference between passing by reference vs. passing by value**](http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Khalil Khalaf Sep 13 '16 at 15:49
  • Local variables disappear after the execution leaves the function. Returning a pointer to a local variable is undefined behavior. – Thomas Matthews Sep 13 '16 at 15:51
  • 2
    If you want the contents of `d` in `main` then why not return it from the function? – NathanOliver Sep 13 '16 at 15:51

1 Answers1

2

You can't just grab memory and expect others to know about it!

The normal way to pass back data from a function is via its return value. Your return type is currently void (no return value). If you wrote double dis(... instead, you could have written return d; at the end of dis.

In main, you could then write std::cout << "dis returned " << dis(a,b,c);

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Oh - okay i'll try that out for sure :D but just to be certain - if i do calculations in one function and then find the address - i can't react the value of this address from another function ? right ? – Holycrabbe Sep 13 '16 at 16:21
  • @Holycrabbe: The address of _what_? Not all addresses are equal. In particular, the address of an object is very different from the address of an ex-object. Remember, objects inside a function only exist while that function is executing. – MSalters Sep 13 '16 at 23:16
  • Okay - now i get it. I just though that i could reach data from my function "div" through pointers from main - but i now realize i can't do it like that. all i wanted to do was to create multiple different functions who calculate some stuff for me so that i only have to call the function to calculate: i the just then didn't want to type in div(a,b,c) but rather to just referee to the location of the result - but i thought the functions would run alongside the program so i'll find a different way to solve my problem :D thanks a lot – Holycrabbe Sep 14 '16 at 15:02