14

When I ran this program:

#include <iostream> int sqr(int&); int main() { int a=5; std::cout<<"Square of (5) is: "<< sqr(a) <<std::endl; std::cout<<"After pass, (a) is: "<< a <<std::endl; return 0; } int sqr(int &x) { x= x*x; } 

I got the following output:

Square of (5) is: 2280716 After pass, (a) is: 25 

What is 2280716? And, how can I get a value returned to sqr(a) while there is no return statement in the function int sqr(int &x)?

Thanks.

2
  • 7
    WTF? Why would you want to write such code? Clearly sqr is a mathematical function and it makes no sense to use the parameter as in-out. Commented Jan 27, 2011 at 11:40
  • 1
    @Ilya Kogan. Just testing how pass by reference works Commented Jan 27, 2011 at 11:47

6 Answers 6

33

Strictly, this causes undefined behavior. In practice, since sqr has return type int, it will always return something, even if no return statement is present. That something can be any int value.

Add a return statement and turn on warnings in your compiler (g++ -Wall, for instance).

int sqr(int &x) { return x = x*x; } 
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for -Wall. (Now, if you'd add -Werror ... no, I can't upvote twice, damn. :-))
It is a little less simple, not returning anything in a function with a return type other than void causes undefined behavior, not just the return of an undefined value. The difference is that stating that it will return an undefined value means that by simply ignoring that value everything will be fine, but that is not so. The program could crash, corrupt the stack...
@David just to clarify the undefined behavior bit and make sure I understand the issue entirely; if, for instance, our return value were a unique_ptr<T>, without a return statement, the function would return garbage in place of a T*, which would then be interpreted by the caller as a valid unique_ptr, and even if you were to ignore it, the pretend unique_ptr's destructor would crash your program(hopefully) while trying to delete its pointee.
8

That's some garbage that will depend on a handful of factors. Likely that's the value stored in memory where the function would put the result if it had a return statement. That memory is left untoched and then read by the caller.

Don't think of it too much - just add a return statement.

Comments

4

Your function sqr() has no return statement. The function has undefined behavior concerning return value. Your first output shows this return value.

The compiler should show a diagnostic though.

try this:

int sqr(int x) { return x*x; } 

1 Comment

g++ -Wall for default warnings. Check out the manual for specific warnings.
3

You are trying to print the return value of sqr(int &x), which is garbage value in this case. But not returning the proper X*X. try returning valid X*X from sqe

int sqr(int &x) { x= x*x; return x;}

Comments

2

If a function is not declared void then you MUST have a return statement telling what should be the value when returning to the caller. If you fail to do so and simply the function end without returning a value the result of calling this function is "Undefined Behavior" that means that your program could do anything (including crashing or deleting everything that is on your hard disk).

Normally if the value is just a simple int you will get funky numbers, but in more complex cases can be a source of big troubles. Just don't do that.

Compilers will normally inform you that you forgot a return statement if properly instructed to do so (i.e. by enabling the maximum warning level). You can omit to return a value only for cases where the function doesn't actually return (i.e. throws an exception or loops forever).

Also, because of a crazy special rule of the C++ language, the function main can end without a return statement despite being declared as returning an int. Don't waste your time your time looking for a logical reason for this exception, there's none.

Comments

1

You need to make a choice between:

1) Passing by reference/value AND return an INT.

2) Passing by reference AND return void.

This choice depends on the purpose of your function.

If you want a function that gives you the square of a number use the first. If you want a function that takes a variable and replaces it by its square, use the second.

So either:

int sqr(int& x) { return x*x; } 

OR

void sqr(int& x) { x= x*x; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.