not returning anything to non void function

D

dev

using a function declared as non void type and not returning anything
to it...
invokes undefined behaviour...
_________________________
now if i write a function like this....

int foo(int a,intb){
/*.......*/
}/*no return*/

int main(){
if(foo(2,3)){}
return 0;
}

is this undefined behaviour??(in this program)
What i want to ask that since function is defined as non void type
return...so some memory location must be der where return of function
must be stored...may be it register,heap or stack...depending upon
implementation...
....
so some value must be der in that location....say any garbage
value....since we r not intrested in execution of if block....so i m
not concerned what i m getting in return....
so..is this correct program...
or m i misinterpreting something...??
__
regards
 
M

mark.bluemel

using a function declared as non void type and not returning anything
to it...
invokes undefined behaviour...
_________________________
now if i write a function like this....

int foo(int a,intb){
/*.......*/

}/*no return*/

int main(){
if(foo(2,3)){}
return 0;

}

is this undefined behaviour??(in this program)
What i want to ask that since function is defined as non void type
return...so some memory location must be der where return of function
must be stored...may be it register,heap or stack...depending upon
implementation...
...
so some value must be der in that location....say any garbage
value....since we r not intrested in execution of if block....so i m
not concerned what i m getting in return....
so..is this correct program...
or m i misinterpreting something...??

I'm impressed that Google Groups offered me the option of having it
(attempt to) translate this posting into english...
 
J

jameskuyper

(e-mail address removed) wrote:
....
I'm impressed that Google Groups offered me the option of having it
(attempt to) translate this posting into english...

I couldn't find that option - how do you invoke it? I don't suppose it
was actually able to improve the misspellings, grammar mistakes, and
incomplete sentences?
 
K

Keith Thompson

dev said:
using a function declared as non void type and not returning anything
to it...
invokes undefined behaviour...

Only if the caller attempts to use the result.
now if i write a function like this....

int foo(int a,intb){
/*.......*/
}/*no return*/

int main(){
if(foo(2,3)){}
return 0;
}

is this undefined behaviour??(in this program)

Yes. The behavior is undefined because the standard doesn't define
the behavior.

You're probably assuming that foo() must return *some* value, even if
it's garbage, and since the caller looks at the value but doesn't do
anything that depends on it, it should be ok.

The standard *could* have been written to allow this. But it would
have been substantially more work to get the wording right, and the
only benefit would be that some really bad code would have defined
behavior rather than undefined behavior.

The solution is simple: if you declare a non-void return type for a
function, be sure that it actually returns a result. There's no good
reason not to do this.
or m i misinterpreting something...??

It would be very helpful if you'd try to write in something
approaching standard English. For example, capitalize the first
letter of each sentence, proper nouns, and the pronoun "I". Don't use
the silly abbreviations for common words that you might find in a text
message, such as "m" for "am". And don't overuse the ellipsis (...);
it's not just decorative, it has an actual meaning.

If English isn't your first language, or even if it is, nobody is
going to criticize you for the occasional error, but if you can spell
"misinterpreting" correctly, presumably you can spell "am" and "I"
correctly. This isn't a chat room.
 
J

jameskuyper

dev said:
using a function declared as non void type and not returning anything
to it...
invokes undefined behaviour...
_________________________
now if i write a function like this....

int foo(int a,intb){
/*.......*/
}/*no return*/

int main(){
if(foo(2,3)){}
return 0;
}

is this undefined behaviour??(in this program)

6.9.1p12: "If the } that terminates a function is reached, and the
value of the function call is used by
the caller, the behavior is undefined."

Unless foo() (or a subroutine thereof) calls abort() or exit() or gets
stuck in an endless loop, the absence of any 'return' statement
guarantees that the '}' which terminates the function will eventually
be reached. main() uses the value of the function call to determine
whether or not to enter the if() block. Therefore, section 6.9.1p12
certainly applies.

Most good compilers are likely to recognize that the block is empty,
that the if() is therefore pointless, and will therefore generate code
equivalent to simply calling foo(2,3) for the sake of any side effects
it may have, without actually using the return value. However, such
optimizations are never mandatory; as written, your code does use the
return value, though in a trivial and pointless fashion.
What i want to ask that since function is defined as non void type
return...so some memory location must be der where return of function
must be stored...may be it register,heap or stack...depending upon
implementation...
...
so some value must be der in that location....say any garbage
value....since we r not intrested in execution of if block....so i m
not concerned what i m getting in return....

I'm sorry, I don't know precisely why the committee chose to make that
undefined behavior, as opposed to an unspecified value. However, if
you're not concerned with the value you're getting in return, don't
write code that uses that value. It's just that simple. If you do use
the value, make sure that one is returned.
so..is this correct program...
No.

or m i misinterpreting something...??

Apparently.
 
D

dev

Only if the caller attempts to use the result.





Yes.  The behavior is undefined because the standard doesn't define
the behavior.

You're probably assuming that foo() must return *some* value, even if
it's garbage, and since the caller looks at the value but doesn't do
anything that depends on it, it should be ok.

The standard *could* have been written to allow this.  But it would
have been substantially more work to get the wording right, and the
only benefit would be that some really bad code would have defined
behavior rather than undefined behavior.

The solution is simple: if you declare a non-void return type for a
function, be sure that it actually returns a result.  There's no good
reason not to do this.


It would be very helpful if you'd try to write in something
approaching standard English.  For example, capitalize the first
letter of each sentence, proper nouns, and the pronoun "I".  Don't use
the silly abbreviations for common words that you might find in a text
message, such as "m" for "am".  And don't overuse the ellipsis (...);
it's not just decorative, it has an actual meaning.

If English isn't your first language, or even if it is, nobody is
going to criticize you for the occasional error, but if you can spell
"misinterpreting" correctly, presumably you can spell "am" and "I"
correctly.  This isn't a chat room.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
It would be very helpful if you'd try to write in something
approaching standard English. For example, capitalize the first
letter of each sentence, proper nouns, and the pronoun "I".

I will keep this in mind from next time.

I will like to know one more thing.
Can you please elaborate some more on this statement:
" "Random garbage" is not necessarily a "value."

-
regards
 
K

Keith Thompson

dev said:
You're probably assuming that foo() must return *some* value, even if
it's garbage, and since the caller looks at the value but doesn't do
anything that depends on it, it should be ok.
[...]

I will like to know one more thing.
Can you please elaborate some more on this statement:
" "Random garbage" is not necessarily a "value."

No, I can't, because that's not what I said.

I guessed that you were assuming that the function would return some
garbage value. (Note that I didn't use the word "random"; the result,
if any, is garbage, but it's not something you could use to simulate
shuffling a deck of cards.) It's likely to behave that way (acting
like it returned some arbitrary and meaningless value) in many
implementations.

But since the *behavior*, not just the returned value, is undefined,
it needn't return a value at all. For example, you could get a trap
representation, which is a bit pattern that doesn't represent any
value. Or the program could crash at that point. Or, in theory, it
could reformat your hard drive. (The latter is unlikely in practice,
but it wouldn't violate the C standard.)
 
C

CBFalconer

dev said:
using a function declared as non void type and not returning
anything to it invokes undefined behaviour.

now if i write a function like this....

int foo(int a,intb){
/*.......*/
}/*no return*/

int main(){
if(foo(2,3)){}
return 0;
}

is this undefined behaviour??(in this program)

Yes. The if statement is only interested in whether or not the
return value from foo is zero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top