How to pass the value of a variable ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to pass the value of a variable ?

First. Sorry for my eng. I'm russ. Second. My problem is - How to pass the value of the HeroHp variable if it is initialized in the main function. If I start working with this variable in the SomeFunc function, then Visual studio says that this is an error because this variable is not initialized in this function. Для русских. Как передать значение переменной HeroHp если она инициализирована в функции main. Если я начну работать с этой переменной в функции SomeFunc , то Visual studio говорит что это ошибка потому что эта переменная не инициализирована в этой функции. https://code.sololearn.com/cuZHXD68b5TF/?ref=app https://code.sololearn.com/cuZHXD68b5TF/?ref=app

2nd Jan 2020, 11:06 PM
Mell Note
Mell Note - avatar
1 Answer
+ 2
You can make like this : #include <iostream> using namespace std; class Hero { public: int HeroHp; }; int SomeFunc(Hero f) { f.HeroHp ++; return f.HeroHp; } int main() { Hero Warrior; Warrior.HeroHp = 100; Warrior.HeroHp= SomeFunc(Warrior); cout <<Warrior.HeroHp; return 0; } =Or like this= #include <iostream> using namespace std; class Hero { public: int HeroHp; }; int SomeFunc(int f) { f ++; return f; } int main() { Hero Warrior; Warrior.HeroHp = 100; Warrior.HeroHp= SomeFunc(Warrior.HeroHp); cout <<Warrior.HeroHp; return 0; }
2nd Jan 2020, 11:29 PM
ycsvenom
ycsvenom - avatar