Sign in to follow this  
Arnack

Really simple C++ Function question

Recommended Posts

I will put in the source code, and in my comments I will ask questions ^^ // function example #include <iostream> using namespace std; int addition (int a, int b) //i'm supposing this is defining a & b under addition { int r; //defining the letter r r=a+b; //giving equation return (r); //ends r? } int main () //begins another function { int z; //defines z z = addition (5,3); /z is now 5, 3 under addition cout << "The result is " << z; //so would it would display 5+3 return 0; //ends function } Is that correct?

Share this post


Link to post
Share on other sites
Actually, I got it from a website. I just asked questions to the side with comments to make sure I know what is going on.

Share this post


Link to post
Share on other sites
Yup, that's all correct. If you want to put addition underneath main in your source code, you'd do it like this:


int addition(int a, int b); // This declares the function

int main()
{ /* code here... */ }

int addition(int a, int b)
{
/* code for addition here */
}



Other than that, your code looks fine. Everything is okay. Also, just so you know, you don't have to store the returned value into a variable, you can just do:

cout << "Value: " << addition(5,3);

But that's up to you, whichever you prefer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Sorry, did you not notice there is a For Beginners forum, at the top of the forum listing? x.x


It says "General Programming
The place for discussing programming issues not related to games."
for the forum board. I am discussing programming issues.

Share this post


Link to post
Share on other sites
Quote:
Original post by Arnack
Quote:
Original post by Zahlman
Sorry, did you not notice there is a For Beginners forum, at the top of the forum listing? x.x


It says "General Programming
The place for discussing programming issues not related to games."
for the forum board. I am discussing programming issues.


By this logic, 95%+ of the other forums are redundant.

The implication is that you should use the most specific forum possible.

And the most specific forum in this case would be "For Beginners".

This is part of the reason it's listed on the very top of the forum list.

Makes sense, no?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this