Simplicity
Simplicity

Reputation: 48916

C++ return value without return statement

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.

Upvotes: 14

Views: 15905

Answers (6)

6502
6502

Reputation: 114481

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.

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363547

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;
}

Upvotes: 33

harper
harper

Reputation: 13690

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;
}

Upvotes: 4

Jack
Jack

Reputation: 110

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;
}

Upvotes: 1

aJ.
aJ.

Reputation: 35450

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;}

Upvotes: 3

sharptooth
sharptooth

Reputation: 170499

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.

Upvotes: 8

Related Questions