So...
in my
int main () function I have some code
is it possible to make another function like
and show that aswell? or do I have to call it like in php or something?Code:my_name ()
{
blah
}
Printable View
So...
in my
int main () function I have some code
is it possible to make another function like
and show that aswell? or do I have to call it like in php or something?Code:my_name ()
{
blah
}
I suggest you read the function tutorial on this site.
Well typically you declare your functions before int main. Then define them after int main. Then you can use them inside main. Like this:
Also remember that in your functions you can't call for a variable that was initialized in main, unless you pass it to the function.Code:int add(int num_one, int num_two);
int main() {
cout<< add (4, 5);
}
int add (int num_one, int num_two) {
int result;
result = num_one + num_two;
return result;
}
Inline functions work a little bit different....and you know, there are tutorials on this stuff.
Code:#include <iostream>
using namespace std;
int calculate(int a, int b)
{
int result = (a + b) * 2;
return result;
}
int main()
{
int num = calculate(10, 20);
cout<<num<<endl;
cout<<"Hit return to exit.";
cin.get(); //to keep the console window open for your viewing pleasure
return 0;
}
Thanks :)
Can someone point me to a good tutorial?
C++ is a fairly big language, and you need to get a recent book that is no more than a couple of years old. Is that an option for you?
a veritable feast of tutorials including one on functions
Possibly, if something Im trying to do doesn't work out, (it should though) then I will be doing this, otherwise I wontQuote:
7stud C++ is a fairly big language, and you need to get a recent book that is no more than a couple of years old. Is that an option for you?