Function issue

Hello everyone,

I'm currently learning c++, so still don't understand everything so please forgive me for my lack of knowledge.
Here is my issue, i'm trying to write a program, which with 3 values given, can give me back 2 informations.
My issue is that it doesnt find z in the B function.
So I tried many things like declaring it in the function B, adding some pointers, creating a new void function to put the values inside instead but so far, nothing worked.

So for my understanding, the variable values in each function are independant from others...

So that's why I come to you, i really hope i can learn here where is my mistake and how to fix it ! Thank you very much in advance for the help you may provide me :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <iostream>
#include <cmath>

using namespace std

double A (double x, double z)
   {
     double resultA = x-z;
     return resultA;
   }

double B (double y)
   {
     double resultB= z+y;    // here is the issue
     return resultB;
   }

double ifnumber()
   {
     double resultA;

     if (resultA<50)
     { cout<<"ok"<<endl; }

     else 
     { cout<<"wrong"<<endl; }
   }

void giveA()
  {cout<<"answer "<<round(A(2,5))<<endl;}

void giveB
  {cout<<"answer "<<round(B(3))<<endl;}

int main()
{
  giveA();
  ifnumber();
  giveB();
  return 0;
}
> My issue is that it doesnt find z in the B function.
> So for my understanding, the variable values in each function are independant from others...
yes.
when you do B(3) then the `y' parameter is assigned the value 3
no mention of `z', so don't know what kind of guess you want there.

can't tell how to fix it, because I have no idea what B() is supposed to do.
1
2
3
4
5
double B (double y, double z)
   {
     double resultB= z+y;    // here is the issue
     return resultB;
   }


1
2
void giveB
  {cout<<"answer "<<round(B(3,5))<<endl;}


Edit:
1
2
3
4
5
6
double ifnumber()
   {
     double resultA;

     if (resultA<50)
     { cout<<"ok"<<endl; }


This is nonsense, because resultA does not have a valid value. As you've deduced in your post, local variables in different functions are completely independent. They are in separate scopes.
You should read up on what the scope of a variable is, how how to return values from functions.

You're currently not returning any value from ifnumber(), despite the return type being double. You have no way of connecting giveA() to ifnumber() -- I suggest returning a value from giveA, and sending that value to ifnumber as a parameter.

https://www.geeksforgeeks.org/scope-of-variables-in-c/
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
@ne555 : thank you for your reply, indeed i understood that it doesn't know what is the 'z' value. The 'B' is just supose to do a calcul using 'x, y and z', and giving me the value back. Here I just put :z' to be the problem cauz however i can solve this, i can do the same for 'x'

@Ganado ; thank you also for your reply. For the if function, resultA, i have no problem with it, i tried different value x and z which gives different values, and the if functio seems to work fine.
I also read your link, if i understand, by adding '::' before 'z' it should work ? I tried it amd or tried adding double ::z; still nothing...Indeed I did what you suggested, put the 'z' value inside the parentheses, and yes it works fine, but if i have 100 functions, where i can only enter 100 times 3 values, instead of 200 times, i would sure prefer to use the way to do it only 100 times ^^

Last edited on
No, ::variable would only be need to explicitly reference a variable in an outer scope. That doesn't apply here. My point was just that a local variable called "x" in one function is a separate variable from a local variable called "x" in another function. They are different variables.

Sounds like you got it working at least for this particular code, but I'm actually not sure what you're asking in the latter part of your post.
Last edited on
Topic archived. No new replies allowed.