Purpose of Return Value

Dont Post Link Msg.

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1187 Views - Last Post: 16 April 2009 - 05:04 AM Rate Topic: -----

#1 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Purpose of Return Value

Posted 13 April 2009 - 09:06 AM

As I read Tutorials about C++ , why do i need to return a value.

Likewise return 0, return 99, or return NULL;

Example:
charSample()
{
	char a;
	
	cout << "Please Enter Your Name";
	cin >> a;
	
	return a;
}


...then if i use void do i need to use a return value,.?
Is This A Good Question/Topic? 0
  • +

Replies To: Purpose of Return Value

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

Re: Purpose of Return Value

Posted 13 April 2009 - 09:52 AM

Example:
charSample()
{
	char a;
	
	cout << "Please Enter Your Name";
	cin >> a;
	
	return a;
}


A return from the function allows you to assign the function to a variable.
char result = charSample();

// Also the charSample should be declared as...
char charSample(); //This way the compiler knows that char is returned.



You do not need a return in a void function. They are usefull to immediately break out of the function to do this all you would need to write is a simple...
return; // That's it!



Also what is nice about returning values in a function is that you can return one value and manipulate another.
char charSample(char &manip);


So in the above statement the function returns a char and also can change the value of manip through the means of a address-of &.
Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Isrelo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-March 09

Re: Purpose of Return Value

Posted 13 April 2009 - 09:59 AM

View Postnerodamsel, on 13 Apr, 2009 - 08:06 AM, said:

...why do i need to return a value.


In some situations a programmer might want to create a function that does a complicated calculation, and have that complicated calculation returned to the programmer in an easier to use format. As an added benefit instead of having to copy and paste the calculation into your code every time you want to do the calculation you only need to call the function instead of copping multiple times. This makes your code easier to maintain and easier to reuse as well.

Example:

int MultipyThreeNumbers(int Variable1, int Variable2, int Variable3)
{
	int answer = 0; // Place to store some tricky calcualtion.
	
	answer = Variable1 * Variable2 * Variable3; // Do some tricky calculation.

	 return answer; // Return some tricky calcuation.
	
}

int main()
{
	// Numbers you want to multiply.
	int number1 = 5;
	int number2 = 5;
	int number3 = 5;

	int multiplyedAnswer = 0; // Answer that is in a more manageable form.

	multiplyedAnswer = MultipyThreeNumbers(number1, number2, number3); // Call to our created function.

	cout << "The answer is " << multiplyedAnswer << endl; // Print out the result.

	return 0;
}




In your code snippet that you provided.

View Postnerodamsel, on 13 Apr, 2009 - 08:06 AM, said:

Example:

charSample()
{
	char a;
	
	cout << "Please Enter Your Name";
	cin >> a;
	
	return a;
}




I would change your code to.


char charSample()
{
	char a;
	
	cout << "Please Enter a letter.";
	cin >> a;
	
	return a;
}




The reason I would change your code to this is because in your example you are wanting to get a name from the user of your application and then return that value so that you can work with it later in your program.

Example:
...
int main()
{
	...
	char letterStorage;
	
	letterStorage = charSample();
	...
}
...



View Postnerodamsel, on 13 Apr, 2009 - 08:06 AM, said:

...then if i use void do i need to use a return value,.?


If you use void you don't need to return a value, but you need to understand that you use void with functions that don't require you to need the information later on in your program. Like an function you could write to print out a menu for a program.

Example:
...
void PrintSomeMessage(char *messageToPrint)
{
	cout << messageToPrint;
}
...



The final reason you might want to use a return value for a function is so that you can do error checking on a set of instructions.

Example:
[code]

bool CheckAnswer(bool someAnswer)
{

if(someAnswer == 42)
{
return true;
}

return false;
}

int main()
{
int number = 47;

bool isTheAnswerRight;

isTheAnswerRight = CheckAnswer(number);

if(isTheAnswerRight == true)
{
...
//do some more functionality.
...
}
}

I hope this helps you see some of the possible uses of a return value. Some of these examples may seem like it would be pointless to do it this way an for the example your probably right, but the principles still are transferable to more complicated situations.
Was This Post Helpful? 0
  • +
  • -

#4 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 05:24 AM

So the point of returning a value is to make easy to program?
Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Purpose of Return Value

Posted 14 April 2009 - 05:27 AM

A function is a way to run the same piece of code repeatedly. By returning values, you can get a value from the function. Consider this:

float smallest(float x, float y) {
    if(x > y) return y;
    else return x;
}

Simple stuff, right? We're returning the value so we can test it later.

Consider how we'd want to get the smallest out of two values.
float return_value = smallest(1.22,20.11);

return_value will now hold the value 1.22, for use later in the program.
Was This Post Helpful? 0
  • +
  • -

#6 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 05:37 AM

So is My Example Correct...

#include <iostream>

using namespace std;

int main()
{
	int var1, var2, ANSWER;
		cout << "Enter Your Numerator";

}



So is My Example Correct...


#include <iostream>

using namespace std;

int main()
{
	int var1, var2, ANSWER;
		cout << "Enter Your Numerator: ";
		cin >> var1;
		cout << "Enter Your Denominator: ";
		cin >> var2;
		ANSWER= var1+var2;
		return ANSWER;

}




so whats the point returning the variable?

This post has been edited by nerodamsel: 14 April 2009 - 05:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Purpose of Return Value

Posted 14 April 2009 - 05:38 AM

Oops, didn't see this bit:

Quote

...then if i use void do i need to use a return value,.?
You only use void if your function won't return anything.

See my example, it returns a float.

You can return any type, including your own classes.
Was This Post Helpful? 0
  • +
  • -

#8 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 05:40 AM

So I need to use..

As your Example:

float return_value = smallest(1.22,20.11);



[type] return_value = variable(null,null);

sorry for being noobie... how can I used the returned value...
can you give me a good noobie example...im not using
it as my assignment Im self Studying..
Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Purpose of Return Value

Posted 14 April 2009 - 05:42 AM

Right. Consider my example. The function returns a float, yes?

When this code is executed:
float return_value = smallest(1.22,20.11);
[known as a function call] the variable known as return_value will hold the return value of the function, which in this case will be 1.22 since it returns the smallest number of the two which are passed to it.

This post has been edited by gabehabe: 14 April 2009 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#10 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 05:46 AM

Can you give me an simple program using that code? to clarify things out..

Ohh..I love it when someone answer my question...
Was This Post Helpful? 0
  • +
  • -

#11 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Purpose of Return Value

Posted 14 April 2009 - 05:50 AM

It's probably my fault, it's been a while since I explained anything here. :P

#include <iostream>

using namespace std;

float smallest(float x, float y) {
    if(x > y) return y; // return y if smaller
    else return x; // otherwise return x
}

int main() {
    float a = smallest(1.22, 20.33); // a will hold 1.22
    float b = smallest(20.33, 1.22); // b will also hold 1.22
    float c = smallest(1.22, 0.11); // c will hold 0.11

    cout << a << endl << b << endl << c;
    return 0;
}

So you can see, we are running the same routine over and over again, simply by calling the function by name. a, b and c will each hold the smallest number passed to the smallest() function, which returns the smallest of the two. :)

This post has been edited by gabehabe: 14 April 2009 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#12 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 06:04 AM

float a = smallest(1.22, 20.33); // a will hold 1.22
float b = smallest(20.33, 1.22); // b will also hold 1.22
float c = smallest(1.22, 0.11); // c will hold 0.11

sorry for this silly question... whats this smallest (x, y)
why do you use two values for 1 variable..

sorry for spam

:: if X is Greater than Y. return Y, if X return X..
Ohh i understand it..can you make a simple one.. just using the simple
integers i hate floating points..
Was This Post Helpful? 0
  • +
  • -

#13 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 06:10 AM

View Postgabehabe, on 14 Apr, 2009 - 04:27 AM, said:

A function is a way to run the same piece of code repeatedly. By returning values, you can get a value from the function. Consider this:

float smallest(float x, float y) {
    if(x > y) return y;
    else return x;
}

Simple stuff, right? We're returning the value so we can test it later.

Consider how we'd want to get the smallest out of two values.
float return_value = smallest(1.22,20.11);

return_value will now hold the value 1.22, for use later in the program.


i got your point.. haha im just confused with the variable x and y...
Was This Post Helpful? 0
  • +
  • -

#14 nerodamsel  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 118
  • Joined: 31-January 09

Re: Purpose of Return Value

Posted 14 April 2009 - 07:33 AM

is my code correct:

#include <iostream>
using namespace std;


int add(int x, int y)
{
return x + y;
}

int main()
{
cout <<add(3,9)<<endl;

cin.get();
return 0;
}


so still i didnt get why do i need to return a value..

if i can do it manualy.. like

cout << 3+ 5;
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Purpose of Return Value

Posted 14 April 2009 - 07:36 AM

If you didn't return anything, in that code example, nothing would be printed.

//returning a value makes this statement:
cout <<add(3,9)<<endl;

//literally when run:
cout << "12" <<endl;



Because 12 is returned from the function into the output stream.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2