function vs. void

Posted By: kornstar

function vs. void - 05/05/15 01:19

Hello everyone, I am fairly new to programming and 3dgs and have been looking through the workshops and many contributed scripts to learn how to make games using this software. I have noticed that many people use void when creating a function and I have been told that void is the same thing as function except that you are not passing any variables through it.

I was just wondering if there are any other differences (or advantages) to using void as opposed to function.

any info on this would be greatly appreciated.
Posted By: Anonymous

Re: function vs. void - 05/05/15 02:01

Please read this thread from a few weeks ago. I found it with the forum search.

http://www.opserver.de/ubb7/ubbthreads.p...true#Post449452

Mal
Posted By: kornstar

Re: function vs. void - 05/05/15 02:25

Thank you very much Malice, I had began to look through some threads to try to figure this out but quickly became overwhelmed by the amount of info that can be found on this forum.

As I am a Noob to this I will have to re-read the thread a few times to properly understand it but it looks like it has all the info I need.

Again, thanks for your help laugh
Posted By: Anonymous

Re: function vs. void - 05/05/15 02:39

Your welcome,
And Welcome to the forum...
Posted By: kornstar

Re: function vs. void - 05/05/15 03:24

Thanks, I look forward to bombarding you all with my noob questions, but I'll try running searches first to avoid double posting questions (like this time).
Posted By: DLively

Re: function vs. void - 05/05/15 14:44

Originally Posted By: MasterQ32
function is just an alias for var.
Both are defined with as fixed:
Code:
typedef fixed var;
typedef fixed function;


So you can actually do this:
Code:
function a, b;
a = 10;
b = 20;
printf("%f", a + b);



Regards
Posted By: kornstar

Re: function vs. void - 05/05/15 15:42

Thanks DLively, I had read that post from your original thread (thanks to the link provided by Malice).

if I understand that statement correctly then I should also be able to go

Code:
var example(){
insert function code here;
}



and it would have the same effect as

Code:
function example(){
insert function code here;
}



and I should be able to call the first example the same way as the second

Code:
example();



is that right or am I not understanding this properly?
Posted By: DLively

Re: function vs. void - 05/05/15 15:55

yes, exactly laugh

and void doens't return values
Quote:
void cant return anything aka 0xC3 in assembler
Posted By: kornstar

Re: function vs. void - 05/05/15 16:19

I haven't run into a situation yet where I have needed to return any values from a function, and to be honest I'm still not really sure how the whole "return" thing works, but I'm sure that I will eventually need to do that and I will have more specific questions when that time comes.

Just out of curiosity... what is 0xC3?.. As I said I'm still very new to this and have a lot to learn.

Thanks again for the help laugh
Posted By: WretchedSid

Re: function vs. void - 05/05/15 16:37

Originally Posted By: kornstar
Just out of curiosity... what is 0xC3?..

It honestly doesn't matter, it's just Chaos trying to sound smart and then ending up mixing multiple buzzwords that have no relation to each other.

If you want to know the full story in short, your code gets compiled down to machine code at some point (usually with various intermediate steps from high level C to machine code). The CPU will then execute the machine code, as that is the one it understands, note though that there is not necessarily a direct conversion between high level C concepts and what the CPU will eventually see. In any case though, the machine code is, well, machine dependent, and the code the CPU can execute is called the ISA, short for instruction set architecture. Your normal desktop CPU that runs Windows runs the x86 or x86-64 ISA, which is an ISA that has grown since the Intel 80386 (released 1986). One of the many instructions the ISA offers is the return instruction which returns back to the caller, and encoded in hex it's 0xc3. It matters fuck all in your every day coding, so you might as well forget everything about it.

Now, for void vs everything else. void is just saying that the function won't return any value, whereas for example int means the function will return an int to the caller.

This is tied to return only insofar that you use return to return a value to the caller and a function that returns anything but void has to have a return with a value somewhere to not be malformed. Of course this would break a lot of badly written Lite-C functions so the compiler doesn't enforce it. You can use return in void functions too though, it just won't return anything. But it can be used to pre-maturely leave a function based on a condition or whatever.

When your function reaches the last closing bracket, there is an implicit return from it.

There is no advantage of one over the other. Some dipshits used to spread the rumour that one is faster than the other, but luckily this hasn't come up in the more recent past. If you find a reference to this anywhere though, just remember that it's absolute bullshit and don't believe anything the author has to say. The real difference is that you provide intent. If you intent to return an int, then declare your function as returning an int. If you don't return anything, use void. That way the compiler can help you out (if it weren't for the fact that it's shit) and you can get an idea of what the function will do based on just looking at the functions signature.
Posted By: Ch40zzC0d3r

Re: function vs. void - 05/05/15 16:48

Theres a difference between C2 and C3...
C2 is used for stack cleanup for the stdcall/thiscall/fastcall calling conventions because its not done by the caller but by the callee. Depending on the return size it could end up on the stack if its > 4 byte because EAX is 4 byte and cant hold a complete structure which is bigger.
And it would be nice if you could stop acting like you know anything and I nothing. You have actually no Idea what I know and what not so please dont post those comments since you actually write the same with just some more words.

And in short: C3 is the command for "RETN" in assembler written in hex.
You dont have to learn any assembler to code with 3dgs, was just saying that void actually changes the compiler behaviour together with the calling convention.
Posted By: WretchedSid

Re: function vs. void - 05/05/15 17:03

Originally Posted By: Ch40zzC0d3r
You have actually no Idea what I know and what not so please dont post those comments since you actually write the same with just some more words.

Except your comments give a good glimpse into the fact that you have no idea what you are talking about. Gamestudio doesn't use the stdcall calling convention, so either you know what you are talking about and chose to deliberately put bullshit down to sound smart, or, well...

Also, you can't return structs in Lite-C. Just saying, cause apparently you think that the result will be on the stack if it is greater than 4 byte. Except there is nothing bigger than 4 byte that you could return and that would end up in an integer register. Double precision floating points are returned on the floating point stack which is 80 byte wide and holds long doubles.

And last but not least, Assembler is not machine code. Assembler has to run through an assembler to turn into machine code, 0xc3 has nothing to do with assembler.

The biggest issue I have is that you say "oh, void is 0xc3 and everything else is 0xc2". In a thread asked in ask Gamestudio. Yeah, that will surely help the beginner asking, amirite? I have nothing against just giving pointers in the right direction, but at least make it possible for someone to find the information they need with your pointer. Being a smartass is alright if you ask me. Just stroking your ego and not adding any value to the discussion is not.

PS: C4 is a plastic explosive that can be triggered with an electrical charge.
Posted By: Ch40zzC0d3r

Re: function vs. void - 05/05/15 17:31

I know lite-c doesnt support this stuff, but when I talk about 3DGS then I usually talk about C++ code because Lite-C is a scripting language in the end.
And yes I sometimes dont offer enough info, that happens when I dont re-read my answers and expect everyone to know the basics
Posted By: WretchedSid

Re: function vs. void - 05/05/15 17:41

Originally Posted By: Ch40zzC0d3r
I usually talk about C++ code because Lite-C is a scripting language in the end.

Cool. Next time you ask about something I will answer you in Haskell since apparently the language doesn't matter. Also, C++ doesn't have a standard defined calling convention, but you'll usually find cdecl. So your 0xc3 and 0xc2 argument is void again.

And Lite-C a scripting language? It's a super stripped down C, alright, but it's not a scripting language.
Posted By: Anonymous

Re: function vs. void - 05/05/15 18:07

Lol love these combative posts. But to help the newbies can we port them over to a Rants? It floods the thread otherwise.

Just asking,
Mal
Posted By: Superku

Re: function vs. void - 05/05/15 18:09

I have to agree with the Shitlord here. Helping is nice and all but I'd say keep it comparatively simple in programming terms and esp. keep it lite-C related, not C++, not Assembler or anything fancy I've never heard of before.
Everything else will just evoke more confusion and, judging from the past, could in the end lead to more lite-C related rumors.
Posted By: Michael_Schwarz

Re: function vs. void - 05/05/15 19:32

REKT checklist
[ ] Not REKT
[x] REKT
[x] REKTangle
[x] SHREKT
[x] REKT-it Ralph
[x] Total REKTall
[x] The Lord of the REKT
[x] The Usual SusREKTs
[x] North by NorthREKT
[x] REKT to the Future
[x] Once Upon a Time in the REKT
[x] The Good, the Bad, and the REKT
[x] LawREKT of Arabia
[x] Tyrannosaurus REKT
[x] eREKTile dysfunction
© 2024 lite-C Forums