Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,534 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19054 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
return void in void function #410424
11/02/12 22:25
11/02/12 22:25
Joined: Sep 2007
Posts: 101
Luxembourg
K
krial057 Offline OP
Member
krial057  Offline OP
Member
K

Joined: Sep 2007
Posts: 101
Luxembourg
Hi,
I have the following problem. You might think, this has no purpose, but just read until the end. This is the code that i want to execute:
Code:
void foo2() {
	return;
}

void foo() {
	return foo2();
}

function main() {
	foo();
}



However this doesn't work in lite-c (Can't convert GETRETV:::VOID)
This works in other c-compilers and i could imagine that it is valid C code.
Now I need a workaround for Lite-C... do you have any ideas.

To make it clear why I need this. I have a macro that automatically creates some overloading of a function:
Code:
#define overload(returnType, overloadFunc) \
returnType overloadFunc(char* text, int style) \
{ \
	return overloadFunc(text, NULL, style) \
} \
returnType overloadFunc(BMAP* img, int style) \
{ \
	return overloadFunc(NULL, img, style) \
}



When i now have a function with void as return type, it won't work:
Code:
void drawTextOrImageWithStyle(char* text, BMAP* img, int style)
{
	
}
overload(void, drawTextOrImageWithStyle)


Re: return void in void function [Re: krial057] #410425
11/02/12 22:34
11/02/12 22:34
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
you can overload functions in lite-c but returning a value in a void-function just makes no sense
void means you have no return type, so what do you want to return then?
you can't do this in c (or at least you shouldn't be able to compile it)#

using int, float, whatever just works fine


Visit my site: www.masterq32.de
Re: return void in void function [Re: krial057] #410426
11/02/12 22:35
11/02/12 22:35
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
Kartoffel Offline
Expert
Kartoffel  Offline
Expert

Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
return doesn't work with voids. (void means that nothing gets returned)
use function instead:
Code:
function foo2() {
	return;
}

function foo() {
	return foo2();
}

function main() {
	foo();
}


Last edited by Kartoffel; 11/02/12 22:35.

POTATO-MAN saves the day! - Random
Re: return void in void function [Re: Kartoffel] #410427
11/02/12 23:35
11/02/12 23:35
Joined: Sep 2007
Posts: 101
Luxembourg
K
krial057 Offline OP
Member
krial057  Offline OP
Member
K

Joined: Sep 2007
Posts: 101
Luxembourg
@Kartoffel:
Ty, i was also thinking about such a solution and I think i will use it(however i'm not too happy about it, because it may cause undefined behaviour if the users saves the result in an expression(this wouldn't be the case if void is used...)

@MasterQ32:
I don't agree that it makes no sense... You think so, because you never needed it xD I looked up if it is allowed in C99, and you are right, it isn't. However in c++ it is:
Quote:
An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid, or as the expression in a return statement (6.6.3) for a function with the return type void.


In c++ it is useful for templates, so why shouldn't it be useful in c for macros?

Anyway, thanks for your help laugh

Re: return void in void function [Re: krial057] #410446
11/03/12 08:44
11/03/12 08:44
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
so then explain please what the function should return
you can't do something like this (afaik):
Code:
void foo()
{
   return (void)anystuff;
}
[...]
void myvar = foo();



so writing
Code:
void foo()
{
return foo2();
}
//should be equal to
void foo()
{
foo2();
return;
}



Visit my site: www.masterq32.de
Re: return void in void function [Re: MasterQ32] #410463
11/03/12 12:08
11/03/12 12:08
Joined: Sep 2007
Posts: 101
Luxembourg
K
krial057 Offline OP
Member
krial057  Offline OP
Member
K

Joined: Sep 2007
Posts: 101
Luxembourg
I'm not sure about it... I think we need a C guru to clear it out.
(I'm not sure at all about this, but I try to explain how I understand it...)
For your 1st example:
Code:
void foo()
{
   return (void)anystuff;
}


In my opinion, this should work. As explained in Wikipedia:
Quote:
The void type serves as a unit type

So, why can't I return that unit type.
For the second part of the code:
Code:
void myvar = foo();


This doesn't work, because:
Quote:
the void type is said to comprise an empty set of values, and the language does not provide any way to declare an object or represent a value with type void


Your second example makes no sense for my use. I need to return the function value, if it is non-void...

Here are also the results if compiled with gcc (no warnings or errors, but that has nothing to say...):
http://ideone.com/BHcj3o
http://ideone.com/KTO19U

Re: return void in void function [Re: krial057] #410465
11/03/12 13:50
11/03/12 13:50
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
i read the definition of void type in wikipedia, also the definition of unit type:

Originally Posted By: http://en.wikipedia.org/wiki/Void_type
A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned value. The void type may also appear as the sole argument of a function prototype to indicate that the function takes no arguments. Note that despite the name, in all of these situations, the void type serves as a unit type, not as a zero or bottom type, even though unlike a real unit type which is a singleton, the void type is said to comprise an empty set of values, and the language does not provide any way to declare an object or represent a value with type void.


Originally Posted By: http://en.wikipedia.org/wiki/Unit_type
In the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information)


following this both examples make no sense
you are returning no information so why using return foo(); ?


Visit my site: www.masterq32.de
Re: return void in void function [Re: MasterQ32] #410467
11/03/12 14:26
11/03/12 14:26
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: MasterQ32
following this both examples make no sense
you are returning no information so why using return foo(); ?

He doesn't actually want to return anything, he just wants his macro to work with void as well without having an extra macro for void functions. Geez, am I the only one actually reading more than the thread title?


Anyways, what does the C standard say to this? On page 47, section 6.3.2.2 you have the following statement about void:
Quote:

The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)

(emphasis by me)

So, if you cast something to void, the result of the cast will indeed be discarded, which is quite easy to test. The following run through Clang will generate no errors, and Krials GCC output back this up as well:
Code:
int test1()
{
	printf("Hello World");
	return 24;
}
void test2()
{
	return (void)test1();
}


("Hello World" will be printed to stdout, confirming that the expression is indeed validated like the C standard mandates).

Now, if this doesn't work in Lite-C: Bad luck for you, it should work.

Last edited by JustSid; 11/03/12 14:32.

Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: return void in void function [Re: WretchedSid] #410468
11/03/12 14:38
11/03/12 14:38
Joined: Sep 2007
Posts: 101
Luxembourg
K
krial057 Offline OP
Member
krial057  Offline OP
Member
K

Joined: Sep 2007
Posts: 101
Luxembourg
Ty, JustSid. You understood my question laugh
So it is a missing feature in lite-c... Do you know a workaround for this in lite-c?(except writing an other macro or making the function fixed instead of void...)

Re: return void in void function [Re: krial057] #410469
11/03/12 14:56
11/03/12 14:56
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Well, you can have the dirty as fuck version which works because by exploiting three things:
1) Lite-C can't return structs on the stack
2) An empty return statement doesn't clear any registers
3) Emptry return statements are valid, even for functions returning something

All you have to do is this:
Code:
int someTestFunction()
{
	return 42;
}

int version1()
{
	someTestFunct();
	return;
}
void version2()
{
	someTestFunct();
	return;
}



The idea should be clear, one word of warning though: This is bound to break someday when the compiler gets updated!

Last edited by JustSid; 11/03/12 15:01.

Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1