473,581 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what value does lack of return or empty "return;" return

Hi,
when a function doesn't specify a return type ,value what value is
returned. In the below programme, the function sample()is returning the
value passed to 'k'.

sample(int);
main()
{
int i = 0,j;
j = sample(0);
printf("%d", j);
getch();
}

sample(k)
{
return;
}

Nov 14 '05 #1
15 6720
Greenhorn wrote:
Hi,
when a function doesn't specify a return type ,value what value is
returned. In the below programme, the function sample()is returning the
value passed to 'k'.

sample(int);
main()
{
int i = 0,j;
j = sample(0);
printf("%d", j); No declaration? (Hint: what are you including?) getch(); No such thing. }

sample(k)
{
return;
}

It's undefined behavior. Could be anything. Or nasal demons might ensue.
(The only exception is that `main()' returns 0 in C99 if no value is
explicitly returned. You're not using C99; your program would have been
rejected for trying to use implicit int.)

Turn up the warning level on your compiler!

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
In message <38************ *@individual.ne t>
Artie Gold <ar*******@aust in.rr.com> wrote:
Greenhorn wrote:
sample(k)
{
return;
}

It's undefined behavior. Could be anything. Or nasal demons might ensue.
(The only exception is that `main()' returns 0 in C99 if no value is
explicitly returned. You're not using C99; your program would have been
rejected for trying to use implicit int.)


It would also have been rejected for that return - a "return" without a value
where one is required is now a constraint violation in C99.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #3
Kevin Bracey wrote:
In message <38************ *@individual.ne t>
Artie Gold <ar*******@aust in.rr.com> wrote:

Greenhorn wrote:
sample(k)
{
return;
}


It's undefined behavior. Could be anything. Or nasal demons might ensue.
(The only exception is that `main()' returns 0 in C99 if no value is
explicitly returned. You're not using C99; your program would have been
rejected for trying to use implicit int.)

It would also have been rejected for that return - a "return" without a value
where one is required is now a constraint violation in C99.

Would it have gotten that far? Seems to me it wouldn't even parse.

--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #4
Artie Gold <ar*******@aust in.rr.com> writes:
Kevin Bracey wrote:
In message <38************ *@individual.ne t>
Artie Gold <ar*******@aust in.rr.com> wrote:
Greenhorn wrote:

sample(k)
{
return;
}
It's undefined behavior. Could be anything. Or nasal demons might
ensue. (The only exception is that `main()' returns 0 in C99 if no
value is explicitly returned. You're not using C99; your program
would have been rejected for trying to use implicit int.)

It would also have been rejected for that return - a "return"
without a value
where one is required is now a constraint violation in C99.

Would it have gotten that far? Seems to me it wouldn't even parse.


"return;" is perfectly legal in a function returning void. It's
a semantic error, not a syntax error.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
Kevin Bracey wrote:
In message <38************ *@individual.ne t>
Artie Gold <ar*******@aust in.rr.com> wrote:
Greenhorn wrote:
>sample(k )
>{
> return;
>}
>

It's undefined behavior. Could be anything. Or nasal demons might
ensue. (The only exception is that `main()' returns 0 in C99 if no
value is explicitly returned. You're not using C99; your program
would have been rejected for trying to use implicit int.)

It would also have been rejected for that return - a "return"
without a value
where one is required is now a constraint violation in C99.


Would it have gotten that far? Seems to me it wouldn't even parse.

"return;" is perfectly legal in a function returning void. It's
a semantic error, not a syntax error.

Of course. But it's either a function returning int in C89 or nonsense
(no return type) in C99. That's what I referring to in my `wouldn't even
parse' comment.

Cheers,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #6
Artie Gold <ar*******@aust in.rr.com> writes:
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes: [...]
Would it have gotten that far? Seems to me it wouldn't even parse.

"return;" is perfectly legal in a function returning void. It's
a semantic error, not a syntax error.

Of course. But it's either a function returning int in C89 or nonsense
(no return type) in C99. That's what I referring to in my `wouldn't
even parse' comment.


The term "parse" refers specifically to *syntactic* analysis.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
[...]
Would it have gotten that far? Seems to me it wouldn't even parse.

"return;" is perfectly legal in a function returning void. It's
a semantic error, not a syntax error.


Of course. But it's either a function returning int in C89 or nonsense
(no return type) in C99. That's what I referring to in my `wouldn't
even parse' comment.

The term "parse" refers specifically to *syntactic* analysis.

Exactly.

And and it seems to me that:

sample(k)
{
return;
}

would choke on either the `(' or the `{'.

Cheers,
--ag


--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #8
Artie Gold wrote:
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
Keith Thompson wrote:

Artie Gold <ar*******@aust in.rr.com> writes:

[...]
> Would it have gotten that far? Seems to me it wouldn't even parse.
"return;" is perfectly legal in a function returning void. It's
a semantic error, not a syntax error.
Of course. But it's either a function returning int in C89 or nonsense
(no return type) in C99. That's what I referring to in my `wouldn't
even parse' comment.


The term "parse" refers specifically to *syntactic* analysis.

Exactly.

And and it seems to me that:

sample(k)
{
return;
}

would choke on either the `(' or the `{'.

Cheers,
--ag

Actually, I take that back -- `sample' itself would make no sense in
that context (it's not a type).

--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #9
Artie Gold <ar*******@aust in.rr.com> writes:
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
Keith Thompson wrote:

Artie Gold <ar*******@aust in.rr.com> writes:

[...]
>Would it have gotten that far? Seems to me it wouldn't even parse.

"return;" is perfectly legal in a function returning void. It's
a semantic error, not a syntax error.
Of course. But it's either a function returning int in C89 or nonsense
(no return type) in C99. That's what I referring to in my `wouldn't
even parse' comment.

The term "parse" refers specifically to *syntactic* analysis.

Exactly.

And and it seems to me that:

sample(k)
{
return;
}

would choke on either the `(' or the `{'.


I feel like we're arguing over the most trivial aspect of this, but ...

Parsing is based on the language grammar. The above is an old-style
definition, but let's make it explicit:

int sample(int k)
{
return;
}

The "return;" is invalid because it doesn't return a value. But this
is perfectly legal:

void sample(int k)
{
return;
}

The *parser*, when it see the "return;", doesn't remember what type
the containing function returns. The distinction between a valid
"return;" in a void function, and an invalid "return;" in an int
function, cannot be made by the parser. It can only be made during
semantic analysis.

Conceivably the grammar could have been defined with one syntax for a
void function definition (allowing "return;") and another for a
non-void function definition (disallowing "return;"). Fortunately,
this wan't necessary, since it's easy enough to detect the error
semantically.

In C99, this restriction is expressed as a constraint in 6.8.6.4p1:

A return statement with an expression shall not appear in a
function whose return type is void. A return statement without an
expression shall only appear in a function whose return type is
void.

In C90, a "return;" in a non-void function is allowed (a throwback to
K&R C, which didn't have void functions), but an attempt to use the
function result causes undefined behavior.

Incidentally, this is a distinction that the standard does not
(clearly) make. Both syntactic and semantic analysis occur during
translation phase 7. An implementation is free to mix the two in any
way it likes.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2587
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
5
4142
by: Grant Nosbush | last post by:
I have searched the newsgroups and the web trying to solve this problem and just can't get it. Maybe someone will be able to help. I am getting the following error when I try to view a test page in my browser. This test page just reads a table in mysql and outputs the results. Fatal error: Call to undefined function: mysql_connect() in...
3
3985
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number without having to implement a "search" function on th games array
1
2010
by: vl106 | last post by:
char* foo () { return "abc"; } I compiled the above code both with MSVC and GCC for PPC. The string "abc" is generated as a global entity. Thus (1) foo doesn't return a temporary and (2) no deallocation is necessary. What does the standard say about this? Is this a "feature" I can rely on on
7
20691
by: Drum2001 | last post by:
I have created a database to track employee time. Within that, I have also created a report off the following query: SELECT ALLTasksFilter.ProjectName, Sum(.NumberOfCompletions) AS SumOfNumberOfCompletions, Sum(.HoursWorked) AS SumOfHoursWorked, .WorkedByWho FROM ALLTasksFilter LEFT JOIN ON ALLTasksFilter.ProjectName = .ProjectName
1
1315
by: Jorgen [DK/2600] | last post by:
Hi, another problem I have is that have compounded fields in my sql table. Example product@customer I need a simple function to return "customer", so it should return the value after "@", unfortunate "@" will sometimes be character number 6, sometimes
3
1543
by: Peter K | last post by:
Hi I am having trouble writing a webservice which is to provide access to the functions of an existing codebase. The existing code may not be changed at all. For example, I obtain an IDataProvider from a factory. IDataProvider has a method "GetCustomer()" which returns an ICustomer object (ICustomer is an interface, and at the level of...
4
1414
by: Wayne Shu | last post by:
Hi everyone: I am reading Bjarne Stroustrup's The C++ Programming Language(Special Edition). In section 7.3, bs wrote "Like the semantics of argument passing, the semantics of function value return are identical to the semantics of initialization. A return statement is considered to initialize an unnamed variable of the returned type."
5
6253
by: stpgmn | last post by:
I'm executing the following procedure within Access mdb. The query that I'm trying to run works nolrmally when executed as Query. Do anybody has any idea of reason of the error?. Thanks a lot Private Sub Form_BeforeInsert(Cancel As Integer) Dim Istruz As Variant
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.