Jump to content
  • Advertisement
Sign in to follow this  
Miguel Chang

Functions Help C++

This topic is 4167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <stdafx.h> #include <IOSTREAM.h> using namespace std; int main() { int a = 10; int b = 20; int answer = add(a, b); cout << "This is the value returned by the function add: " << answer << endl; return 0; } int add(int i, int j) { return i + j; // return statement } Is it legal to declare i and j in a function and have those values turn into a and b by returning a value from the function add()? How does this happen? Need a little clarification please

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Miguel Chang
Is it legal to declare i and j in a function and have those values turn into a and b by returning a value from the function add()?

How does this happen? Need a little clarification please


I'm not quite sure what you mean. As you're code is now, it probably won't compile because you need to either define or declare your add function before main(). Also, the correct c++ header is <iostream>, not <IOSTREAM.h>.

If your code were to compile, it would do something like the following:
Create ints a and b, and assign to them the values of 10 and 20, respectively.
Then it would call add, which would have two variables of its own, i and j. These variables would be copies of a and b, and as such, would have the values of a and b.
The add function would add i and j to get 30, and return that value. This value would then be stored in the int answer.

Share this post


Link to post
Share on other sites
And for the sake of good habits

int add(int i, int j)

should be either

int add(const int i, const int j)

or

int add(const int& i, const int& j)

Share this post


Link to post
Share on other sites
Quote:
Original post by LorenzoGatti
And for the sake of good habits

int add(int i, int j)

should be either

int add(const int i, const int j)

or

int add(const int& i, const int& j)
Um, what? No it shouldn't. Types less than or equal to the native type size (which is int) should be passed by value unless they're to be modified by the function. Passing by constant value is redundant.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Passing by constant value is redundant.


Well it's not quite redundant because it implies that not only will the passed parameters not be changed, but also the variables won't be changed inside the function (they're const local variables). But it is silly [smile].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!