Hello!
I'm working on my first big project (big for me) and I have an issue. How can I transfer the value from a function to another one?
I'll give you an example:
#include <iostream>
using namespace std;
int verify(int x, int val){
if(x>10)val=100;
else val=50;
}
int main()
{
int x,val=0;
cin >> x;
verify(x,val);
cout << val;
}
If i introduce "400" i want to show on the screen 100, but it shows 0. I need to transfer the value 'val' from function "veirify" in the value 'val' from function "main".
I really hope you can help me and sorry for english mistakes :)
Thank You ;-)
First off you have verify returning a value and you are not using it. One way to get the value is to return instead of passing in a return parameter for example:
#include <iostream>
int verify(int x);
int main()
{
int x = 50;
int val = verify(x);
std::cout << val << std::endl;
return 0;
}
int verify(int x)
{
if(x > 10)
return 100;
return 50;
}
However, I think you want to know how to pass parameters to functions and have them maintained their updated values. In C++ parameters are passed by value not reference. That means that any change to the value of a variable that you pass by value will not be retained at the end of the calling function. As this is a C++ learning forum I am sure there is much material on pass by value and pass by reference. The latter is what you want to use.
#include <iostream>
void verify(int x, int &val);
int main()
{
int x = 50;
int val = 0;
verify( x, val);
std::cout << val << std::endl;
return 0;
}
void verify(int x, int &val)
{
if (x > 10)
val = 100;
else
val = 50;
}
I changed the return type to void because you are not using a return at this point and I am not sure it would be meaningful. The C style way of doing this would be using a pointer to create a pass by reference and that would look like the following:
#include <iostream>
void verify(int x, int *val);
int main()
{
int x = 50;
int val = 0;
verify( x, &val);
std::cout << val << std::endl;
return 0;
}
void verify(int x, int *val)
{
if (x > 10)
*val = 100;
else
*val = 50;
}
The previous way is better. If the function is only going to compare a number to a value and then return something it's better to use a return value and it's clearer (the first example).
While I am new this forum, first day and second post, I think it's considered proper etiquette to use the code formatting blocks when asking folks to read your code. Others would know better than I do but I'm pretty sure on that one. It's the formatting icon that has "<>" on it.
A star in C/C++ means pointer. It means this variable refers to an address where a value is stored.
If you write the code:
int * val;
That means val contains the address of an integer value. Once it's assigned you would use the * to de-reference to get the value contained at that address.
1 2
std::cout << val << std::endl; // This will print the address contained in the variable
std::cout << *val << std::endl; // This will print the value contained at the address
I put it in for completeness to help you understand the notion of pass by value and pass by reference. It isn't the version you should use. It's good to know this as you look at code but for your current question use the reference version in the second example.
2 Questions.
Would the address of the integer variable be where you declared it?
Also what is it to de-reference something.
If you have exploned these already im very sorry, im a slow learner and a very much noob at c++ but im not the worst, i am not very good at learning from writing hence the reason why i might have missed stuff, apologies in advace if this did occur