C programming

A

arnobbd

What is the cause of the warning message after compiling most of the
programs,"function must return a value"?

posted by: Md. Asif Hossain.
 
R

Richard Tobin

What is the cause of the warning message after compiling most of the
programs,"function must return a value"?

Presumably you have a function that is declared to return a value,
but doesn't have a return statement.

Look at the function referred to in the error message. If you
still can't see what's wrong, post it so we can look at it.

-- Richard
 
U

user923005

What is the cause of the warning message after compiling most of the
programs,"function must return a value"?

This is caused by a function that is supposed to return a value (IOW,
a non-void function) that fails to return a value.
It's a programmer bug (IOW *you -- the programmer* did something
wrong).
 
E

Eric Sosman

user923005 said:
This is caused by a function that is supposed to return a value (IOW,
a non-void function) that fails to return a value.
It's a programmer bug (IOW *you -- the programmer* did something
wrong).

It's possible he's working with old code, from the
days before `void' entered the language. In those days
a function returning no value would be written like

hello() {
puts ("Hello, world!");
}

Formally, leaving off the return type is not equivalent
to the modern `void hello()' or `void hello(void)', but is
actually equivalent to `int hello()'. The function returns
no value, but there wasn't a way to say so; callers were
just expected to know they shouldn't try to pluck a value
from the hello() function.

Fast-forward to 1989 and the ANSI Standard, which introduced
the notation `void hello()' as an explicit statement that the
function returns no value. This improved things a little,
because now if a caller tried to use the non-existent value
the compiler could complain, and the compiler could also
complain if somebody added `return 42;' in the function body.
But the old notation was still supported so that thousands
of programmers wouldn't waste millions of hours in busy-work
stuffing `void' keywords into working code.

Still, it's an anomaly for a function that is defined as
"I return an int" not to return a value at all. The compiler
may simply be helping out by pointing out a suspicious feature,
something the O.P.'s attention ought to be drawn to, a sort
of mechanical "Are you *sure* you want to do this?" If so, the
warning can be ignored -- but a better cure would be to change
`hello() {...}' to `void hello(void) {...}' (and alter any
declarations to match).

Fast-forward yet again to 1999, and the second major version
of what was by then the ISO Standard finally retired the old
"implicit int" notation. Under the 1999 rules, the old-style
`hello() {...}' is not merely suspect but forbidden. So if the
code is to keep working as the language is now defined, the advice
to "Add `void'" becomes mandatory, not optional.

Or, of course, the O.P.'s code may have some other problem.
 
G

Guest

What is the cause of the warning message after compiling most of the
programs,"function must return a value"?

the compiler detected a constraint violation and generated a
diagnostic.

quarks, it's all quarks
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top