#include <iostream>
usingnamespace std;
int convert(int number);
int main(int number)
{
convert(number);
cout << "the number is "<< number;
}
int convert(int number = 5)
{
number += 5;
return number;
}
im just trying to figure out how to pass a variable to and from a function (where the value is changed from the function) and passed to the calling function
shouldn't this take number from main and pass it to convert function (which adds 5 to it) then goes back to main and displays 10?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int convert(int number);
int main()
{
int number = 5;
convert(number);
cout << "the number is "<< number;
}
int convert(int number)
{
number += 5;
return number;
}
#include <iostream>
usingnamespace std;
int convert(int number);
int main()
{
int number = 100;
cout << "the number is " << convert(number) <<endl;
cout << "the number is "<< number <<endl;
}
int convert(int number)
{
number += 15;
return number;
}
Yes and No if you want to display the number then yes but if you want to assign the value then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int Converter (int Number);
int main ()
{
int Number = 0;
Number = Converter (Number);
cout << Number << endl;
cout << "Press ENTER to end the program." << endl;
cin.get ();
return 0;
}
int Converter (int Number)
{
return Number += 5;
}
I believe you'll want to display your original number before you convert it, as well as have the program sleep for a few seconds so that it doesn't close as soon as it's finished.
I know it was said before but you either seemed to not notice it or else you had ignored it.
int main() will be expecting an integer to be returned so you have to make sure to put in return 0; at the end of the main function (or something similar).
#include <iostream>
usingnamespace std;
int convert(int number);
int main(int number){
cout << "the number is "<< convert(number);
system("PAUSE");
return 0;
}
int convert(int number = 5)
{
number += 5;
return number;
}