Really simple C++ Function question
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?
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.
Yup, that's all correct. If you want to put addition underneath main in your source code, you'd do it like this:
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.
int addition(int a, int b); // This declares the functionint 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.
Sorry, did you not notice there is a For Beginners forum, at the top of the forum listing? x.x
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.
Quote:Original post by ArnackQuote: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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement