Simple function question.

Started by
3 comments, last by bluenotebooks 17 years, 4 months ago
When a the value of a variable is sent into a function is the value that is returned assigned to the variable that was sent into the function? I'm not sure if I am wording this correctly at all. Here is two simple pieces of code, the first creates the desired result (which is squaring a number) the second does not. The second simply prints the number which was originally input by the user. I don't understand this. //square a number input by the user #include <iostream> int square(int x) { return (x * x); } int main() { using namespace std; int number; cout << "Please enter a number you would like to have squared: "; cin >> number; cout << "Your number squared is: " << square(number) << "\n"; return 0; } ________________________________________________________________________________ //square a number input by the user #include <iostream> int square(int x) { return (x * x); } int main() { using namespace std; int number; cout << "Please enter a number you would like to have squared: "; cin >> number; square(number); cout << "Your number squared is: " << number << "\n"; return 0; }
Advertisement
well, this is because the way a function works.

the "return" command you call right before the function ends, actually RETURNS something to whatever you'll ask the compiler to. simply put, you instruct the function to give something away, to return that squared value.

for example in

int now = ctime()

you instruct the compiler to put in the "now" what the ctime function returns, that is, the current time.

in the first example, you actually do something with the returned value. you send it to cout <<.

in the second example, you don't. the function is called, the square number is processed, but the returned value is not kept. you will have to do something like this:

int squared_number=square(x)
cout << squared_number.


If you will want the function to actually store the returned result into the variable you'll pass it, you'll have to study pointers and dereferencing, a subject which is not easy if you are a beginner.

best of all,
izua
What happens when you call square, is that a copy of the parameter is made, then the function body is executed with that copy (no change to the original parameter), then the call to square is replaced by whatever is returned (copy*copy).

So your first case is simply a way of saying:
//square a number input by the user#include <iostream>int main(){using namespace std;int number;cout << "Please enter a number you would like to have squared: ";cin >> number;int a = number*number;cout << "Your number squared is: " << a<< "\n";return 0;}

While your second example is a way of saying:
//square a number input by the user#include <iostream>int main(){using namespace std;int number;cout << "Please enter a number you would like to have squared: ";cin >> number;int a = number*number;cout << "Your number squared is: " << number << "\n";return 0;}
Quote:
When a the value of a variable is sent into a function is the value that is returned assigned to the variable that was sent into the function?


No. This can be done if you wish by using references.

A function makes a copy of its arguments, and returns a copy of its return type.

So:

int z = 2;
int y = square( z );

z is still 2, and y is now 4. There was a temporary variable "x" inside square, this variable was created with the value that z has.

So even if you had coded the function like so:
int square( int x ){    x = x * x; // or x *= x;    return x;}

z would still have the value 2.




Thanks for the replies guys. That sorted things out for me. Much appreciated.

I'm definitely a beginner, just started yesterday. Fun stuff so far.

This topic is closed to new replies.

Advertisement