non-void function

R

Roman Mashak

Hello, All!

Assume I wrote function like:

/* authenticate client */
int cli_auth(char *user_id, char *user_pass)
{
MYSQL conn;
if ( db_init(&conn) == 0 ) {
if ( db_search(&conn, user_id, user_pass) != AUTH_PASS ) return
AUTH_FAIL;
}
else return FAIL;
}

....
if ( cli_auth(msg_id, msg_passwd) == AUTH_FAIL ) {
...
}

Compiling it with gcc, I get the warnings:

#gcc -g -Wall -o server server.c
server.c: In function `cli_auth':
server.c:33: warning: control reaches end of non-void function

Seems like standard requires non-void function to return something, in my
case function always returns some value. Then, why do I get warning?

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
A

Artie Gold

Roman said:
Hello, All!

Assume I wrote function like:

/* authenticate client */
int cli_auth(char *user_id, char *user_pass)
{
MYSQL conn;
if ( db_init(&conn) == 0 ) {
if ( db_search(&conn, user_id, user_pass) != AUTH_PASS ) return
AUTH_FAIL;
What happens if the above condition is not true?
Answer: there is no return value!

....and there's where you get the warning.
}
else return FAIL;
}

...
if ( cli_auth(msg_id, msg_passwd) == AUTH_FAIL ) {
...
}

Compiling it with gcc, I get the warnings:

#gcc -g -Wall -o server server.c
server.c: In function `cli_auth':
server.c:33: warning: control reaches end of non-void function

Seems like standard requires non-void function to return something, in my
case function always returns some value. Then, why do I get warning?
Ah, but it doesn't. See above.

HTH,
--ag
 
P

Peter Nilsson

Roman said:
Hello, All!

Assume I wrote function like:

/* authenticate client */
int cli_auth(char *user_id, char *user_pass)
{
MYSQL conn;
if ( db_init(&conn) == 0 ) {
if ( db_search(&conn, user_id, user_pass) != AUTH_PASS )
return AUTH_FAIL;

If db_init is 0 but db_search returns AUTH_PASS, then your function
does not return a value.
}
else return FAIL;

This else condition only covers the case where db_init is non-zero.
}

...
if ( cli_auth(msg_id, msg_passwd) == AUTH_FAIL ) {
...
}

Compiling it with gcc, I get the warnings:

#gcc -g -Wall -o server server.c
server.c: In function `cli_auth':
server.c:33: warning: control reaches end of non-void function

Seems like standard requires non-void function to return something,

It doesn't require it in _all_ cases, but the behaviour is undefined if
the calling function expects a return value when one isn't supplied.
in my case function always returns some value.

Look again.
Then, why do I get warning?

The C standards has required diagnostics, but otherwise doesn't
prevent a conforming compiler from issuing warnings for whatever
else reason it feels like. You're lucky that gcc has supplied you
with such a warning in this case.
 
R

Roman Mashak

Thank you very much for replies, I got the point and fixed the code.


With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
K

Kenneth Brody

Roman Mashak wrote:
[...]
int cli_auth(char *user_id, char *user_pass)
{
MYSQL conn;
if ( db_init(&conn) == 0 ) {
if ( db_search(&conn, user_id, user_pass) != AUTH_PASS ) return
AUTH_FAIL;
}
else return FAIL;
} [...]
Compiling it with gcc, I get the warnings:

#gcc -g -Wall -o server server.c
server.c: In function `cli_auth':
server.c:33: warning: control reaches end of non-void function

Seems like standard requires non-void function to return something, in my
case function always returns some value. Then, why do I get warning?

What happens if db_init() returns zero, and db_search() returns AUTH_PASS?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top