473,671 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

value returned from a function

Hi all,

if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.

the following function returns the value of sum correctly.

int sum1(int a,int b)
{

int sum;

sum=a+b;

//return sum;
}

Is there some rule defined?
Thanks,
Noor

Aug 4 '06 #1
9 1758
"no********@gma il.com" <no********@gma il.comwrote:
if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.
Anything random.
the following function returns the value of sum correctly.
No, it doesn't. It may seem, by accident, to do so on the implementation
you're using, but you're treading on a minefield.
int sum1(int a,int b)
{

int sum;

sum=a+b;

//return sum;
}

Is there some rule defined?
No. None whatsoever. It's not even guaranteed not to crash the computer.

Richard
Aug 4 '06 #2
Hi Richard,

Thanks.

But I tried with three compilers ,VC,Turbo C and gcc. in all the three
cases I got the answer correct.

I found out by experiment that it returns the integer on which the
operation happens first.
for example.

int sum(int a,int b)
{

int sum,k;

k=a-5;

sum=a+b;

}

this returns the value of k correctly.

Thanks
Noor

Richard Bos wrote:
"no********@gma il.com" <no********@gma il.comwrote:
if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.

Anything random.
the following function returns the value of sum correctly.

No, it doesn't. It may seem, by accident, to do so on the implementation
you're using, but you're treading on a minefield.
int sum1(int a,int b)
{

int sum;

sum=a+b;

//return sum;
}

Is there some rule defined?

No. None whatsoever. It's not even guaranteed not to crash the computer.

Richard
Aug 4 '06 #3

no********@gmai l.com wrote:
>
I found out by experiment that it returns the integer on which the
operation happens first.
As others have stated, you can't depend on what gets returned. On some
systems, the return value comes back in a particular register, say ax.
Now jsut by chance, many compilers do arithmetic using the ax rgister
(on the x86, "ax" is the "main" arithmetic register, as the multiply
and divide operations cqan only be done on ax). So quite often you'll
find a arithmetic result in ax.

But not always-- a compiler is perfectly free to use a different
register, or trash ax after the arithmetic op, so you can't depend on
ax having any particular value. Just adding a statement, anywhere in
the function, can change ax. Or changing the optimization level, or
adding or subtracting a declaration can change it. So DONT DEPEND on
the function result getting set to anything reliable.
But other compilers return function results as the top item on the
stack, which if you don't set the return value, usually has something
unpredictable in it.

Aug 4 '06 #4
no********@gmai l.com said:
Hi Richard,

Thanks.

But I tried with three compilers ,VC,Turbo C and gcc. in all the three
cases I got the answer correct.
No, you didn't. In all three cases you got the same result. That doesn't
mean you got the answer correct.
I found out by experiment that it returns the integer on which the
operation happens first.
A perfect example of the dangers of experimentation .

The experiment would have been useful if it had led you to ask: "I have a
function that is defined as returning an int value but which doesn't
actually do so. I observed identical behaviour on three different compilers
- in each case, it does <foo>. Is <fooactually guaranteed by the
language, or is this just a coincidence?"

And, of course, the answer is that it is just a coincidence, and is
certainly not guaranteed by the language definition. It is unwise to rely
on this behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 4 '06 #5
"Ancient_Hacker " <gr**@comcast.n etwrites:
(on the x86, "ax" is the "main" arithmetic register, as the multiply
and divide operations cqan only be done on ax)
The latter "fact" is not true. IMUL has forms with one, two, and
three operands. The two- and three-operand versions don't
necessarily involve AX. (MUL always involves the accumulator.)

This is why you shouldn't post off-topic stuff to comp.lang.c.
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
Aug 4 '06 #6

no********@gmai l.com wrote:
Hi all,

if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.

the following function returns the value of sum correctly.

int sum1(int a,int b)
{

int sum;

sum=a+b;

//return sum;
}

Is there some rule defined?
Indeed, it invokes *undefined* behavior; it may appear to work
correctly, or it may return a garbage value, or it may do something
else entirely.

Add another statement after the addition (such as a printf() statement)
and see if you still get the result you expect.
Thanks,
Noor
Aug 4 '06 #7
"John Bode" <jo*******@my-deja.comwrites:
no********@gmai l.com wrote:
> if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.

the following function returns the value of sum correctly.

int sum1(int a,int b)
{

int sum;

sum=a+b;

//return sum;
}

Is there some rule defined?

Indeed, it invokes *undefined* behavior; it may appear to work
correctly, or it may return a garbage value, or it may do something
else entirely.

Add another statement after the addition (such as a printf() statement)
and see if you still get the result you expect.
If I recall correctly, falling off the end of the function doesn't
invoke undefined behavior, it merely returns an indeterminate result.
Attempting to *use* that result invokes UB.

--
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.
Aug 4 '06 #8
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:
>
"no********@gma il.com" <no********@gma il.comwrote:
> if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.
[...]
>Is there some rule defined?

No. None whatsoever. It's not even guaranteed not to crash the computer.
In C99, it's a constraint violation and thus need not even compile.

-Larry Jones

These things just seem to happen. -- Calvin
Aug 4 '06 #9
la************@ ugs.com writes:
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:
>"no********@gm ail.com" <no********@gma il.comwrote:
>> if I declare a function as one returning int and do not return
anything from the function ,what will it return? for example.
[...]
>>Is there some rule defined?

No. None whatsoever. It's not even guaranteed not to crash the computer.

In C99, it's a constraint violation and thus need not even compile.
Um, I don't think so.

N1124 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.

N1124 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.

So this program neither violates a constraint nor invokes undefined
behavior (though it's horribly bad style):

int foo(void)
{
/* no return */
}

int main(void)
{
foo();
return 0;
}

Elsethread, I said that the value returned is indeterminate; I don't
think that's correct, though the effect is similar. If it were merely
indeterminate, then one might argue that, if foo() returned an
unsigned char, the caller could safely use the value, since unsigned
char has no trap representations . In fact, the behavior is undefined
becusae the standard says so.

From a programmer's point of view, all these details are, or should
be, irrelevant. Just Don't Do That.

--
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.
Aug 4 '06 #10

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

Similar topics

20
10138
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
9
11907
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is up to which size m would you expect vector<double> returns_by_value() {
8
3073
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
25
2926
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
21
3963
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
8
13997
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on stack. this contains the arguments this function was called with, address to return to after return from this function (the location in the previous stack frame), location of previous frame on stack (base or start of this frame) and local variables...
6
2168
by: David N | last post by:
Hi All, What is a best way to handle an undefined value object class that returned from a function. I have a function that call the ADO.NET ExecuteScalar() function and returns the object to the calling function as follow: public object FunctionA() { object RetVal;
3
2140
by: Divick | last post by:
I was reading this section in Bruce Eckel's book which talks about passing and returning large objects ( Chapter 11: References & the Copy-Constructor ), where he explains that how to return big objects / values from functions and the issues involved therein specifically w.r.t. reentrency. He explains that because of reentrency, the object to be returned can't be located down in the stack (down in relative here, which means that stack...
9
3714
by: DanielJohnson | last post by:
I am wondering where does the value returned by main goes. Suppoes main returns some number say 42, where is it stored. Supposed a shell script call a C program and the program returns the value 42 where is this value stored in memory ?
7
10317
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function IsLvItemCheckedDelegate(ByVal ClientID As Integer) As Boolean Private Function IsLvItemChecked(ByVal ClientID As Integer) As Boolean If lvServers.InvokeRequired = True Then lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf IsLvItemChecked),...
0
8472
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8596
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2806
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
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.