Jump to content
  • Advertisement
Sign in to follow this  
blade5

ok so some clarification on the Return? c++

This topic is 3411 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

So i am reading Sams teach youreslf c++ in one hour a day. So far the only thing i'm a bit confused about is the Return function. Anyone have a better explanation perhaps? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Well if you are asking what I think you are then this may help.

Return is used to end a function, you return to the original function that called it. When you use return you can specify a value to return to the previous function if you wanted. For instance:


iWillCallAFunction()
{
int i = 0;

i = thisFunctionDoesSomething();

cout << "The value of the variable i is: " << i << endl; // when ran the output of i will end up being 1.
}

int thisFunctionDoesSomething()
{
int i=0;

i += 1;

return i;
}

Share this post


Link to post
Share on other sites
hmm I didn't mean like return 0;. Forgot about that bad explaining on my part.

Ok so like here's a code that I wrote I understand everything except why that return is there.

Quote:

#include <iostream>
int Subtract (int first, int second)
{
std::cout << "Subtract received " << first << " and "<< second <<"\n";
return (first - second);
}

int main()
{
using std::cout;
using std::cin;

cout << "I'm in a main()!\n";
int a, b, c;
cout << "Enter two numbers: ";
cin >> a;
cin >> b;
cout << "\nCalling Add()\n";
c=Subtract(a,b);
cout << "\nBack in main().\n";
cout << "c was set to " << c;
cout << "\nExiting...\n\n";
return 0;
}

Share this post


Link to post
Share on other sites
I've typed out a C++ program for you to look at which shows you how return works.


#include <iostream>

int Add_My_Number();

int main()
{
int Number = 5;

Number += Add_My_Number();

std::cout << Number;

std::cin.get();

return 0;
}

int Add_My_Number()
{
int Five = 5;

return Five;
}





Here is another example on how return works to see if you have enough Gold.


#include <iostream>

int Check_Gold(int Gold_Temp);

int main()
{
int Gold = 5;

bool Enough_Gold = false;

Enough_Gold = Check_Gold(Gold);

if (Enough_Gold == true)
{
std::cout << "Enough Gold!";
}
else
{
std::cout << "Not Enough Gold!";
}

std::cin.get();

return 0;
}

int Check_Gold(int Gold_Temp)
{
if (Gold_Temp == 5)
{
return true;
}
else
{
return false;
}
}





If you change Gold to 4 and re run the program, it will say "Not Enough Gold!".

Now to answer your other question on what that code is doing.

Quote:
Original post by blade5
hmm I didn't mean like return 0;. Forgot about that bad explaining on my part.

Ok so like here's a code that I wrote I understand everything except why that return is there.

Quote:

#include <iostream>
int Subtract (int first, int second)
{
std::cout << "Subtract received " << first << " and "<< second <<"\n";
return (first - second);
}

int main()
{
using std::cout;
using std::cin;

cout << "I'm in a main()!\n";
int a, b, c;
cout << "Enter two numbers: ";
cin >> a;
cin >> b;
cout << "\nCalling Add()\n";
c=Subtract(a,b);
cout << "\nBack in main().\n";
cout << "c was set to " << c;
cout << "\nExiting...\n\n";
return 0;
}


What is happening here, is that a and b are going in as copies into your function Subtract, a acts as first, and b acts as second. The return value of (first - second) will get copied into your variable c.

I hope this helped! Good luck!

Share this post


Link to post
Share on other sites
No problem. Once you get into pointers you can make direct changes without using return. For example:


#include <iostream>

void Change_Number(int *Ptr_Number_Temp);

int main()
{
int Number = 10;
int *Ptr_Number = &Number;

std::cout << Number << std::endl;

Change_Number(Ptr_Number);

std::cout << Number;

std::cin.get();

return 0;
}

void Change_Number(int *Ptr_Number_Temp)
{
*Ptr_Number_Temp = 20;
}




However, for now just get into the understanding of working with copies and using return. Pointers can cause a lot of problems for people who are new, however it allows you to get out of scope restrictions, unless of course you globalize everything.

Share this post


Link to post
Share on other sites
Yea I have been reading that here and seeing that from alot of people. I'm hoping I can understand it when I get that far but for now just one step at a time :)

Share this post


Link to post
Share on other sites
It's always good to take something like C++ at your own pace so you understand what you're doing. I remember a few months back, someone posted their code for help and had set new all over the place without one delete, and because of that persons lack of understanding, they allocated memory all over the place without releasing it. Major memory leakage! C++ is very powerful, yet it's extremely easy to screw up and cause chaos.

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!