473,806 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function automatically returns the value

hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69

Thanks All

by
chellappa

Nov 23 '05 #1
12 1960
chellappa said:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69


foo@bar:~/scratch> ./foo
ENTER ANY THREE NUMBERS :
TWENTY-THREE TWENTY-THREE TWENTY-THREE
sum = 45768

--
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)
Nov 23 '05 #2
"chellappa" <N.*********@gm ail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
hi All,
function automatically
accidentally
returns the value,am not used "return"
i am using Linux -gcc complier
Then you're using a *very old* version, or have
it configured to ignore serious errors, or use
some very strange extension(s).
please tell me.... what is problem...
Undefined behavior.
source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
This is archaic syntax, not valid standard C.

calsum(int x, int y, int z) // C89

int calsum(int x, int y, int z) // C99
{
int d;
d=x+y+z;
}
Undefined behavior. return statement required.

What probably happened for you was that the value of
'd' just accidentally happened to get stored in a
register which was also accessed by the calling
function. The standard C language can't be used
to explain the behavior because you don't have
a standard C program. If you want to inquire
about the quirks of gcc, see a gcc group.
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69

-Mike
Nov 23 '05 #3
chellappa wrote:

hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69


The problem is that your program works by accident, since calsum does
not explicitly return a value. On another implementation it may not.
In general, when your code doesn't conform to the standard (and
implementation specifications) you get undefined behavior. That
occurs with your program. It is particularly nasty because it may
work in some cases, or implementations , and not others.

A powerful technique for avoiding those cases is to enable all
warnings and heed them. You should have gotten appropriate warnings
for your code.

--
Thad
Nov 23 '05 #4
Mike Wahler <mk******@mkwah ler.net> wrote:
Then you're using a *very old* version, or have
it configured to ignore serious errors, or use
some very strange extension(s).


Or he's getting better at trolling...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #5

"Richard Heathfield" <in*****@invali d.invalid> wrote in message
news:dm******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...
chellappa said:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69


foo@bar:~/scratch> ./foo
ENTER ANY THREE NUMBERS :
TWENTY-THREE TWENTY-THREE TWENTY-THREE
sum = 45768


I get output of "ELEVENTY JILLION AND SIX", followed by my
speakers emitting the faint sound of coyotes howling
in the distance. And my nose is beginning to itch.
Maybe th%)v^ *H)^_()&./7[NO CARRIER]

Nov 23 '05 #6
"Mike Wahler" <mk******@mkwah ler.net> writes:
"chellappa" <N.*********@gm ail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...

[...]
source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;


This is archaic syntax, not valid standard C.


Yes, it's archaic, but it's still legal (except that C99 forbids
implicit int). But of course there's no good reason not to use
prototypes.

--
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 23 '05 #7
chellappa wrote:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...


#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}

calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}

In C, functions that have not defined return value,
do return value of int.
So, following prototypes are identical:

int func (int x, char b);

func (int x, char b);

If you want to declare a function without return value,
you use void:

/* A function that doesn't return a value. */
void func (int x, char b);
Nov 23 '05 #8
Tatu Portin <ax****@mbnet.f i> writes:
chellappa wrote:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b, c);
printf("sum = %d\n", sum);
}

calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}

In C, functions that have not defined return value,
do return value of int.
So, following prototypes are identical:

int func (int x, char b);

func (int x, char b);


That's true in C90, but not in C99 -- and it's a bad idea in either.
If you want to declare a function without return value,
you use void:

/* A function that doesn't return a value. */
void func (int x, char b);


Yes, of course, but since chellappa was assigning the result of the
function call to a variable, he obviously wanted the calsum() function
to return a value. The fix is not to change the function so it
returns void; the fix is to add a return statement (and to use proper
prototypes rather than the archaic, but still legal, pre-ANSI function
declarations, and to fix a few other problems).

The original question was why the incorrect code was still "working"
(i.e., the value of x+y+z was assigned to sum even though there was no
return statement). The general answer is that the program invokes
undefined behavior, and the observed behavior is as legitimate as any
other behavior. The specific answer probably has to do with how the
particular compiler uses CPU registers, but quite frankly there's
little reason to care.

--
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 23 '05 #9
Mike Wahler wrote:
"chellappa" <N.*********@gm ail.com> wrote in message
#include <stdio.h>
main()
{
...
sum=calsum(a,b, c);
...
}
calsum(x,y,z)
int x,y,z;
This is archaic syntax, not valid standard C.


As others have stated, it's valid C89.
calsum(int x, int y, int z) // C89
// comments are not valid C89. ;)
int calsum(int x, int y, int z) // C99
{
int d;
d=x+y+z;
}


Undefined behavior. return statement required.


The return statement is required because the calling function attempts
to
use the value.

C99 did not remove the 'feature' that, in general, non-void functions
are not
required to return a value.

--
Peter

Nov 23 '05 #10

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

Similar topics

9
4967
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
9
13912
by: Marek Lewczuk | last post by:
Hello, I'm moving out from MySQL to PostgreSQL and there are some function which are not supported in PG so I'm trying to write my own functions. Currently I have big problem with function IF(), below the description of this function from MySQL manual. Anybody can help me with this ?? I think that PLPGSQL language can be used or maybe other (plPerl) etc.
4
3636
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
7
1728
by: chellappa | last post by:
hi this program return value automatically ... how it is possible ..i am not return any value... but i return correct values i am using Linux -gcc complier please tell me what is this main() { int a,b,c,sum; printf("ENTER ANY THREE NUMBERS :\n"); scanf("%d%d%d",&a,&b,&c);
5
3435
by: Rob Meade | last post by:
Hi all, Quick question if I may... I have a function which depending on whether it was succesful to do something or not returns True or False as a boolean. I have another function which call this function (infact there are two functions as described above)...
11
2779
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print "YAHHH" .... result =
16
11290
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
17
3266
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
7
5066
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new Array(); this.values = new Array();
0
9719
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
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10110
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
9187
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...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
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
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.