Jump to content

Return values in function (C++)

BigCake
Go to solution Solved by Unimportant,
3 minutes ago, Atalia Chez said:
<snip>

 

A function is simply a subroutine that can be called from another piece of code and optionally takes parameters (input values) to work with and, optionally, returns a value (output value).

	int sum(int firstnum, int secondnum)
//	^1  ^2  ^3  ^4        ^5  ^6     

Declares a function called sum (2), that takes 2 parameters by value, the first parameter of type int (3),  called firstnum (4). The second parameter also of type int (5), called secondnum (6).

The function returns a int (1).

 

Taking parameters by value means firstnum and secondnum will be copies of the original parameters passed to the function when it's called.

 

int sum(int firstnum, int secondnum)
{
  	//Define a new variable of type int, called 'result', which is initialised to 0. 
    //This variable will only exist from now on and until the end of function sum. When the function returns it ceases to exist.
	int result=0;				
  
  	//Add firstnum and secondnum together and assign the result to the variable 'result'
 	result=firstnum+secondnum;
  
  	//Return a copy of the value in variable 'result' to the caller.
 	return result;
}
 
//the 'main' function, taking no parameters, and returning a int to the operating system, known as the 'exit code'
int main()
{
  	//Create new variable of type int called 'multiply', which is initialised to 2. 
	int multiply=2;
  
  	//Create new variable of type int called 'mainResult', which is uninitialised. (contains garbage)
 	int mainResult;
  
  	//Call the function/subroutine called 'sum', and pass it the parameters 253 as 'firstnum' and 456 as 'secondnum'.
    	//The result, which 'sum' returns, will be multiplied with variable 'multiply'. 
  	//The result of that is assigned to variable 'mainResult'
 	mainResult=multiply*sum(253,456);
  
  	//return 0 to OS, indicates success.
 	return 0;
} 

 

Hi guys. With no experience with any programming language, lately I have started learning C++.Now I am stuck at a point. I don't understand how the return function works. Can someone please teach me why it exists, what it does and how does it work?. 

 

I know I am noob :(

Link to comment
Share on other sites

Link to post
Share on other sites

It returns a value to the calling function, like so

 

int myFunction() {

return 1;

}

 

int i = myFunction();

 

i will now be equal to 1

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

The parameters of a function let you know what is being brought down to the function where the return statement lets you know what comes back up from the function.

 

So when I assign a function to a variable, like:

int function() {
  return 5;
}

int number = function();

then whatever ever is returned is assigned to the variable.

 

The return function doesn't make much sense until you have several blocks of code interelated.  

 

Edit: Ryan just beat me to it...

Edited by newgeneral10
Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Ryan_Vickers said:

It returns a value to the calling function, like so

 

int myFunction() {

return 1;

}

 

int i = myFunction();

 

i will now be equal to 1

Okay, what I understand is that basically whatever value you assign to return, it will add that value to function, in your case "myFunction();" I understood what you said.

 

#include <iostream>
using namespace std;
int sum(int firstnum, int secondnum)
{
	int result=0;
	result=firstnum+secondnum;
	return result;
}

int main()

{
	int multiply=2;
	int mainResult;
	mainResult=multiply*sum(253,456);
	reutrn 0;
}

^^^ I don't really understand what is going on in above code. I copied it from a tutorial. Could you please explain step by step how it is working? :3

4 minutes ago, newgeneral10 said:

The parameters of a function let you know what is being brought down to the function where the return statement lets you know what comes back up from the function.

 

So when I assign a function to a variable, like:


int function() {
  return 5;
}

int number = function();

then whatever ever is returned is assigned to the variable.

 

The return function doesn't make much sense until you have several blocks of code interelated.  

 

Edit: Ryan just beat me to it...

Could you explain the code that I have shared above? :3

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Atalia Chez said:

Okay, what I understand is that basically whatever value you assign to return, it will add that value to function, in your case "myFunction();" I understood what you said.

 

^^^ I don't really understand what is going on in above code. I copied it from a tutorial. Could you please explain step by step how it is working? :3

It's doing 253 + 456 and then multiplying the result by 2.  You will never see that number though since it is never displayed

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

So basically it runs the main function first.  It only runs the other functions when they're called.  Then you create the multiply variable of type int with a value of 2.  You then create another int variable called mainResult with no value.  The value you assign to mainResult would be the value of multiply (which is 2) times whatever is returned from the sum function.  To find this return value you call the sum function with the values of 253 and 456.  The sum function just adds them and returns them.  Now that you have the return value of 709 that you can multiply with the multiply variable (poor choice of variable names).  mainResult now equals 2*709 or 1418.  The return statement of the main function doesn't really have a use.  I think its mainly used as like an error status but you can ignore that one for all intents and purposes.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, newgeneral10 said:

So basically it runs the main function first.  It only runs the other functions when they're called.  Then you create the multiply variable of type int with a value of 2.  You then create another int variable called mainResult with no value.  The value you assign to mainResult would be the value of multiply (which is 2) times whatever is returned from the sum function.  To find this return value you call the sum function with the values of 253 and 456.  The sum function just adds them and returns them.  Now that you have the return value of 709 that you can multiply with the multiply variable (poor choice of variable names).  mainResult now equals 2*709 or 1418.  The return statement of the main function doesn't really have a use.  I think its mainly used as like an error status but you can ignore that one for all intents and purposes.

This is what I have understood so far.

*******************************************************

#include <iostream>
using namespace std;
int sum(int firstnum, int secondnum)
{
 int result=0;
 result=firstnum+secondnum;
 return result;
}
 

*******************************************************

 

In the above segment of code, I have started a function named as 'sum' and in this function I have started 2 other variables. Then a variable is initialized by the name of 'result' and given a value of 0. Now this variable 'result' equals to the sum of previously defined 2 variables namely 'firstnum' and 'secondnum'. That last line of code written "return result;" is whats bugging me. Does it means that the result variable will be returned to "sum" function? Is this how it works?

 

 

********************************************************

 

int main()
{
 int multiply=2;
 int mainResult;
 mainResult=multiply*sum(253,456);
 return 0;
}
 
*******************************************************
 
Now in this second segment of code, in the main function, 2 new variables are created namely 'multiply' and 'mainResult'. Now the mainResult equals to the value of 'multiply' times 253 and 456 of firstnum and secondnum respectively.
Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Atalia Chez said:
<snip>

 

A function is simply a subroutine that can be called from another piece of code and optionally takes parameters (input values) to work with and, optionally, returns a value (output value).

	int sum(int firstnum, int secondnum)
//	^1  ^2  ^3  ^4        ^5  ^6     

Declares a function called sum (2), that takes 2 parameters by value, the first parameter of type int (3),  called firstnum (4). The second parameter also of type int (5), called secondnum (6).

The function returns a int (1).

 

Taking parameters by value means firstnum and secondnum will be copies of the original parameters passed to the function when it's called.

 

int sum(int firstnum, int secondnum)
{
  	//Define a new variable of type int, called 'result', which is initialised to 0. 
    //This variable will only exist from now on and until the end of function sum. When the function returns it ceases to exist.
	int result=0;				
  
  	//Add firstnum and secondnum together and assign the result to the variable 'result'
 	result=firstnum+secondnum;
  
  	//Return a copy of the value in variable 'result' to the caller.
 	return result;
}
 
//the 'main' function, taking no parameters, and returning a int to the operating system, known as the 'exit code'
int main()
{
  	//Create new variable of type int called 'multiply', which is initialised to 2. 
	int multiply=2;
  
  	//Create new variable of type int called 'mainResult', which is uninitialised. (contains garbage)
 	int mainResult;
  
  	//Call the function/subroutine called 'sum', and pass it the parameters 253 as 'firstnum' and 456 as 'secondnum'.
    	//The result, which 'sum' returns, will be multiplied with variable 'multiply'. 
  	//The result of that is assigned to variable 'mainResult'
 	mainResult=multiply*sum(253,456);
  
  	//return 0 to OS, indicates success.
 	return 0;
} 

 

Link to comment
Share on other sites

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

×