473,562 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i want to use functions that don't return int, without declaring them

Hi,

If I want to call functions that don't return int without declaring
them, will there be any harm?

I only want to assign the function(return value) to the type that it
returns, so I don't see how the return value comes to play here.
Ex
int main
{
double val;

val = func();
/* when return value comes here, its a double */

return EXIT_SUCCESS;
}
double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/
}

I know why we have prototypes, but declarations I have no clue how they
are useful. The actual function's code will convert the return value
to the proper type, in the call location if we(programmer) know what
type the function is returning, then why do we need declarations? How
do they help?

Nov 14 '05 #1
16 1975
On 2005-02-16 19:22:01 -0500, "G Patel" <ga********@gma il.com> said:
Hi,

If I want to call functions that don't return int without declaring
them, will there be any harm?

I only want to assign the function(return value) to the type that it
returns, so I don't see how the return value comes to play here.
This is undefined behavior, don't do it. It may work by chance on some
platforms, but you have no guarantee of that.

Imagine, for instance, a platform that has seperate floating point
registers and integer registers that are used to return function results
Ex

int main
{
double val;
On this hypothetical platform, the compiler says "hey, this function
func must be 'int func()', so I should look for the return value in the
integer register".
It calls the function, and fetches the value in the integer register
(which is likely garbage).
It then converts that garbage int value to a double, val now has a
garbage value.
val = func();
/* when return value comes here, its a double */

return EXIT_SUCCESS;
}
double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/
Yes it will, and on our hypothetical platform, it stuffs that value
into the floating point register, while leaving the integer register
with an undefined value. }

....Or none of that could happen, such is the nature of undefined
behavior. Moral of the story: don't do it.

Also, note that your code won't even compile on a C99 compiler where
implicit int is no longer supported.
I know why we have prototypes, but declarations I have no clue how they
are useful. The actual function's code will convert the return value
to the proper type, in the call location if we(programmer) know what
type the function is returning, then why do we need declarations? How
do they help?


Because without them, your program exhibits undefined behavior. Period.

--
Clark S. Cox, III
cl*******@gmail .com

Nov 14 '05 #2

G Patel wrote:
Hi,

If I want to call functions that don't return int without declaring
them,
Why?
will there be any harm?


It's illegal and your compiler should issue a diagnostic for an
undeclared function. Only functions returning int could be undeclared
under the old standard. As of C99, that's not even legal.

Brian

Nov 14 '05 #3
G Patel wrote:

If I want to call functions that don't return int without
declaring them, will there be any harm?
Yes.
I only want to assign the function(return value) to the type
that it returns, so I don't see how the return value comes to
play here.

int main
Not a good example.
{
double val;

val = func();
/* when return value comes here, its a double */
No. It comes as an int, becasue that's what you've implicitly
told the compiler. Note that your code is a constraint violation
under C99.
return EXIT_SUCCESS;
}

double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/
}

I know why we have prototypes,
It seems that you don't.
but declarations I have no clue how they are useful.
The actual function's code will convert the return value
to the proper type, in the call location if we(programmer)
know what type the function is returning, then why do we need
declarations? How do they help?


Implementations typically have a precise mechanism for returning
function values. This may be a stack or specific register. The
calling function only knows the return mechanism by knowing
the (possibly implied) function signature.

More pragmatically: func may return an 8 byte value on a stack,
or it may return a value through a floating point register. Given
an implicit int return type, the calling function may assume that
func returns a 4 byte int on the stack, or a value through an
integer register.

--
Peter

Nov 14 '05 #4
In article <20050216193213 16807%clarkcox3 @gmailcom>,
Clark S. Cox III <cl*******@gmai l.com> wrote:
:Imagine, for instance, a platform that has seperate floating point
:registers and integer registers that are used to return function results

That hypothetical is, as I recall, instantiated for MIPS R4x00/
R8000/ R1000/ R12000 ABI on SGI's IRIX, and probably other
systems as well.

The IRIX mips3/mips4 ABI also has rules for returning small structures
in registers. Which is, incidently, a point that the OP missed: that
structures can be returned as results, and if you don't declare that as
your return type, you are fairly unlikely to just happen to get
everything squished into the right registers.
--
Can a statement be self-referential without knowing it?
Nov 14 '05 #5

Clark S. Cox III wrote:
On 2005-02-16 19:22:01 -0500, "G Patel" <ga********@gma il.com> said:
Hi,

If I want to call functions that don't return int without declaring
them, will there be any harm?

I only want to assign the function(return value) to the type that it returns, so I don't see how the return value comes to play here.
This is undefined behavior, don't do it. It may work by chance on

some platforms, but you have no guarantee of that.

Imagine, for instance, a platform that has seperate floating point
registers and integer registers that are used to return function results

Ohh, makes sense now. But now how do non-prototyped function calls
handle parameter passing. Someone communication is made between the
call and the actual function code. Parameter passing works without a
prototype, why can't a return variable work without a declaration?

Ex

int main
{
double val;
On this hypothetical platform, the compiler says "hey, this function
func must be 'int func()', so I should look for the return value in

the integer register".
It calls the function, and fetches the value in the integer register
(which is likely garbage).
It then converts that garbage int value to a double, val now has a
garbage value.
val = func();
/* when return value comes here, its a double */

return EXIT_SUCCESS;
}
double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/


Yes it will, and on our hypothetical platform, it stuffs that value
into the floating point register, while leaving the integer register
with an undefined value.
}


Yes, but parameter passing without declarations would be a "bigger"
case of what you are saying, but C compilers handle it properly.
Ex.

double func(); /* parameters not specified */

int main(void)
{
double i;

i = func(4, 6);

return EXIT_SUCCESS;
}

/* in another file */
double func(short x, short y)
{
return (double)x + y;
}
/* This works fine, even though main doesn't know anything about func's
parameters. It still manages to call func. How come return values are
more tricky? */

Nov 14 '05 #6

G Patel wrote:
Clark S. Cox III wrote:
On 2005-02-16 19:22:01 -0500, "G Patel" <ga********@gma il.com> said:
Hi,

If I want to call functions that don't return int without declaring them, will there be any harm?

I only want to assign the function(return value) to the type that it returns, so I don't see how the return value comes to play here.
This is undefined behavior, don't do it. It may work by chance on

some
platforms, but you have no guarantee of that.

Imagine, for instance, a platform that has seperate floating point
registers and integer registers that are used to return function

results

Ohh, makes sense now. But now how do non-prototyped function calls
handle parameter passing. Someone communication is made between the
call and the actual function code. Parameter passing works without a
prototype, why can't a return variable work without a declaration?

Ex

int main
{
double val;


On this hypothetical platform, the compiler says "hey, this

function func must be 'int func()', so I should look for the return value in

the
integer register".
It calls the function, and fetches the value in the integer register (which is likely garbage).
It then converts that garbage int value to a double, val now has a
garbage value.
val = func();
/* when return value comes here, its a double */

return EXIT_SUCCESS;
}
double func(void)
{
return 5;
/* this will convert 5 to a double value before passing
back*/
Yes it will, and on our hypothetical platform, it stuffs that value
into the floating point register, while leaving the integer register with an undefined value.
}


Yes, but parameter passing without declarations would be a "bigger"
case of what you are saying, but C compilers handle it properly.
Ex.

double func(); /* parameters not specified */

int main(void)
{
double i;

i = func(4, 6);

return EXIT_SUCCESS;
}

/* in another file */
double func(short x, short y)
{
return (double)x + y;
}
/* This works fine, even though main doesn't know anything about

func's parameters. It still manages to call func. How come return values are more tricky? */

Because parameters/arguments are passed on the stack and the method is
well defined. Return values are less well defined (can be passed back
many different ways, depends on the type).

Nov 14 '05 #7
"G Patel" <ga********@gma il.com> writes:
[...]
Ohh, makes sense now. But now how do non-prototyped function calls
handle parameter passing. Someone communication is made between the
call and the actual function code. Parameter passing works without a
prototype, why can't a return variable work without a declaration?
No, parameter passing doesn't necessarily work correctly without a
prototype. With no prototype in scope, the compiler will make certain
assumptions about the types of the parameters, based on the types of
the arguments that you pass. For example, an argument of type short
will be promoted to int, and an argument of type float will be
promoted to double. If this isn't what the called function is
expecting, you get undefined behavior -- which includes working just
as you expect.

[...] Yes, but parameter passing without declarations would be a "bigger"
case of what you are saying, but C compilers handle it properly.
Ex.

double func(); /* parameters not specified */

int main(void)
{
double i;

i = func(4, 6);

return EXIT_SUCCESS;
}

/* in another file */
double func(short x, short y)
{
return (double)x + y;
}
/* This works fine, even though main doesn't know anything about func's
parameters. It still manages to call func. How come return values are
more tricky? */


The integer constants 4 and 6 are of type int, and are passed that
way. The function expects to receive arguments of type short. If int
and short arguments happen to be passed by the same mechanism (e.g.,
if they're both passed in registers), your program may happen to work.
(It will likely continue to work until it breaks at the most
inconvenient possible moment.)

Incidentally, the program above is not what you actually compiled.
The macro EXIT_SUCCESS is defined in <stdlib.h>; if you're using it,
you must have a "#include <stdlib.h>" that you didn't show us. I'm
guessing your actual program also had a printf() call that displayed
the result of your function call (and, I hope, a "#include <stdio.h"),
leading you to believe that the compiler handled it "properly".

In this particular case it was easy enough to figure out what you
meant, but in general it's a good idea to post a complete compilable
program (preferably a small one); otherwise we'll get hung up on the
differences between what you posted and what you actually compiled.

--
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 #8
On 2005-02-16 21:02:32 -0500, ro******@ibd.nr c-cnrc.gc.ca (Walter
Roberson) said:
In article <20050216193213 16807%clarkcox3 @gmailcom>,
Clark S. Cox III <cl*******@gmai l.com> wrote:
:Imagine, for instance, a platform that has seperate floating point
:registers and integer registers that are used to return function
results

That hypothetical is, as I recall, instantiated for MIPS R4x00/
R8000/ R1000/ R12000 ABI on SGI's IRIX, and probably other
systems as well.


I know, PowerPC is the one I had in mind, but I wanted to keep from
using specific implementations as an example.
--
Clark S. Cox, III
cl*******@gmail .com

Nov 14 '05 #9
On 2005-02-16 21:03:44 -0500, "G Patel" <ga********@gma il.com> said:

Yes, but parameter passing without declarations would be a "bigger"
case of what you are saying, but C compilers handle it properly.
Ex.

double func(); /* parameters not specified */

int main(void)
{
double i;

i = func(4, 6);

return EXIT_SUCCESS;
}

/* in another file */
double func(short x, short y)
{
return (double)x + y;
}
/* This works fine, even though main doesn't know anything about func's
parameters. It still manages to call func. How come return values are
more tricky? */


They aren't more tricky, it's the exact same issue. If it works for
you, you just got lucky. It's entirely possible that int and short just
happen to be passed in the same way on your platform, and everything
just happens to line up, but, again, you can't rely on that.
--
Clark S. Cox, III
cl*******@gmail .com

Nov 14 '05 #10

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

Similar topics

2
4595
by: will taubin | last post by:
i would like my program to have a config.php with passwords and other stuff. i would like to have a functions.php to hold all my functions. i would like the functions.php to include/require the config.php as it will need it for database stuff. i cant seem to get the functions in my functions.php to see the variables in config.php
19
4230
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
110
8513
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm almost halfway through writing the over 200 functions needed to implement C99's version of math.h, and I would like to have some feedback and/or...
7
2562
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope) global variables outside of the files they were defined it, and created some files that just hold global variables definitions (just variables,...
1
1988
by: Ilya N. Golubev | last post by:
There is already one function processing pointer values this way. And there may easily appear more, including wrappers for this function. For purposes of further discussion, consider simplified function, `consume'. It has one arg, INBUF, that points to a variable that points to the first character in the input buffer. The function never...
4
1995
by: Steven T. Hatton | last post by:
<quote url="http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=53&rl=1"> exported Templates Last updated Sep 6, 2006. exported Templates The separate compilation model enables us to define functions, types and objects in one translation unit and refer to them from other translation
10
4778
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
23
3497
by: Anil Gupte | last post by:
I am trying to set up a function that connects to the database that I can then use gloablly. I set up a class called L3Global in which I have a function as follows: Public Function SetDBConnect() Dim ConnStrL3Producer As String ConnStrL3Producer = "User ID=UID;Tag with column collation when possible=False;Data Source=" & Chr(34) &...
17
3512
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
8101
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...
1
7627
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
7935
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
6221
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...
0
5193
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...
0
3623
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...
1
2073
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
1
1191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
903
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...

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.