How do I return early from a void function?

D

Daniel Rudy

Hello Everyone,

How do I return early from a void function? The following code
does not compile.

strata:/home/dr2867/c/modules 735 $$$ ->./compile unpifi.c unpifi
gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wnested-externs -Wwrite-strings -Wflo
at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -lc -o unpifi unpifi.c
unpifi.c: In function `ifi_link_address':
unpifi.c:146: error: syntax error before ')' token
unpifi.c:146: warning: `return' with a value, in function returning void
unpifi.c:150: error: syntax error before ')' token
unpifi.c:150: warning: `return' with a value, in function returning void

strata:/home/dr2867/c/modules 736 $$$ ->



/* convert ethernet hardware address to a human readable
form.
internal holding buffer is 32 bytes.
*dest - pointer to result buffer.
*src - pointer to binary address.
size - size of binary address. 6 or 8 */
void ifi_link_address(char *dest, uchar *src, int size)
{
uchar *ptr;
int i;
char buff1[32];
char buff2[32];

/* check for input errors */
if (dest == NULL) return();
if (src == NULL)
{
dest[0] = '\0';
return();
}
if ((i = size) > 0)
{
ptr = src;
bzero(buff1, sizeof(buff1));
bzero(buff2, sizeof(buff2));
do
{
snprintf(buff1, sizeof(buff1), "%s%02x", (i == size) ? " " : ":", *ptr++);
strlcat(buff2, buff1, sizeof(buff1));
} while (--i > 0);
memcpy(dest, buff2, strlen(buff2) + 1);
}
else dest[0] = '\0';
}


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
I

Ian Collins

Daniel said:
Hello Everyone,

How do I return early from a void function? The following code
does not compile.

strata:/home/dr2867/c/modules 735 $$$ ->./compile unpifi.c unpifi
gcc -W -Wall -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wnested-externs -Wwrite-strings -Wflo
at-equal -Winline -Wtrigraphs -ansi -std=c89 -pedantic -ggdb3 -lc -o unpifi unpifi.c
unpifi.c: In function `ifi_link_address':
unpifi.c:146: error: syntax error before ')' token
unpifi.c:146: warning: `return' with a value, in function returning void
unpifi.c:150: error: syntax error before ')' token
unpifi.c:150: warning: `return' with a value, in function returning void
The compiler is telling you that return isn't a function. If provided,
the parenthesis are associated with the return value. A void function
doesn't have a return value, so the parenthesis are a syntax error.

Just use return;
 
D

Daniel Rudy

At about the time of 1/24/2007 9:55 PM, Ian Collins stated the following:
The compiler is telling you that return isn't a function. If provided,
the parenthesis are associated with the return value. A void function
doesn't have a return value, so the parenthesis are a syntax error.

Just use return;

Thank you for the help. I've been out of programming for
about 5 months now. After 3-4 days back into it, it's
coming back, but these little things keep getting me.

Thank you again for your help.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
M

Martin Ambuhl

Daniel said:
Hello Everyone,

How do I return early from a void function? The following code
does not compile. [...]


return();
^^^^^^^^

'return' is not a function. A simple 'return;' would do.
 
K

Keith Thompson

Daniel Rudy said:
How do I return early from a void function? The following code
does not compile. [snip]
void ifi_link_address(char *dest, uchar *src, int size)
{ [snip]
return();
[snip]
}

It's "return;", not "return()".


A return statement, even one that returns a value, does not require
parentheses. For example, "return 0;" is legal (in a function
returning an appropriate type); "return(0);" is also legal, but the
argument to the return is a parenthesized expression "(0)".
 
D

Daniel Rudy

At about the time of 1/24/2007 10:40 PM, Keith Thompson stated the
following:
Daniel Rudy said:
How do I return early from a void function? The following code
does not compile. [snip]
void ifi_link_address(char *dest, uchar *src, int size)
{ [snip]
return();
[snip]
}

It's "return;", not "return()".


A return statement, even one that returns a value, does not require
parentheses. For example, "return 0;" is legal (in a function
returning an appropriate type); "return(0);" is also legal, but the
argument to the return is a parenthesized expression "(0)".

Interesting. I've always used return(<value/variable>); inside
parentheses instead of just using return; or return 0;. That's good to
know. So I guess that return is a reserved keyword then?

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
S

santosh

Daniel said:
At about the time of 1/24/2007 10:40 PM, Keith Thompson stated the
following:
Daniel Rudy said:
How do I return early from a void function? The following code
does not compile. [snip]
void ifi_link_address(char *dest, uchar *src, int size)
{ [snip]
return();
[snip]
}

It's "return;", not "return()".


A return statement, even one that returns a value, does not require
parentheses. For example, "return 0;" is legal (in a function
returning an appropriate type); "return(0);" is also legal, but the
argument to the return is a parenthesized expression "(0)".

Interesting. I've always used return(<value/variable>); inside
parentheses instead of just using return; or return 0;. That's good to
know. So I guess that return is a reserved keyword then?

Yes.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top