Notifications
Clear all

Let's start an argument about arguments!  

  RSS

itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 4 weeks ago
Posts: 44
2020-05-14 6:33 am  

I'm curious. Back when I actually cared about things like this, I researched to find that arguments in the C language were read by the compiler from the back going forward (from right to left), but there are so many versions of C and compilers to match these days, I'm no longer sure.

What I'm curious the most about right now is Arduino. Would anyone out there happen to know if the compiler attached to the Arduino IDE reads arguments from right to left, or left to right?

Thanks in advance.


Quote
Robo Pi
(@robo-pi)
Robotics Engineer Moderator
Joined: 12 months ago
Posts: 1744
2020-05-14 8:06 am  

I don't know the answer, but I have a suggestion:

If you can think of a situation where it would make a difference, then all you need to do to discover the answer is to try writing the code both ways and see which one produces the results you expect, and which one does not.

On the other hand, if you can't dream up a scenario where it would make a difference, then doesn't that make the question somewhat moot?

I'm sure there are differences in the way it passes data and bytes, etc.   But again, those would be very easy to discover by writing simple programs and looking at the output.   The output will either be what you expect, or the exact opposite.

 

DroneBot Workshop Robotics Engineer
James


ReplyQuote
starnovice
(@starnovice)
Estimable Member
Joined: 12 months ago
Posts: 159
2020-05-14 8:22 am  

I'm pretty sure it reads them left to right, but they get pushed on a stack so the right most one is the first one available. So it can seem like it reads them right to left.

Pat Wicker (Portland, OR, USA)


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 12 months ago
Posts: 452
2020-05-14 11:48 am  
Posted by: @itscaseydambit

I'm curious. Back when I actually cared about things like this, I researched to find that arguments in the C language were read by the compiler from the back going forward (from right to left), but there are so many versions of C and compilers to match these days, I'm no longer sure.

What I'm curious the most about right now is Arduino. Would anyone out there happen to know if the compiler attached to the Arduino IDE reads arguments from right to left, or left to right?

Thanks in advance.

Arduino uses a cut down version of the ISO C++ language, which I would think, must adhere to the evaluation order of arguments as originally designed into the language.

Do you have any reason to think otherwise?

 


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 4 weeks ago
Posts: 44
2020-05-15 3:09 am  

@robo-pi

That would be one approach (the result from output), but I'm not sure if it would answer my question, unless we could put a stopwatch on each argument being pushed on the stack.

@starnovice called it right. I was curious which argument was pushed on the stack first (I didn't phrase the question well).

Here's the thing: I have a habit of placing function calls in my arguments. Examaple: 

myStepper.setSpeed( map( analogRead( SPEEDPOT_PIN ), 0x00, 0x3ff, 0x7f, 0x3ff ) );

So, as you can see, when you throw function calls into the arguments, one function call may affect the values used in another function call, and then the return values are used as arguments, and, depending on exactly how you (brace) the arguments has a reflection on which argument is operated on first.

It can get nasty if things aren't done in the right order, hence, knowing which argument is first examined (for lack of a better word) by the function matters at times.

Hell, I've even confused myself on that one LOL.

Peace always. 🧐 


ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Eminent Member
Joined: 4 weeks ago
Posts: 44
2020-05-15 4:49 am  

@frogandtoad

I have no reason to think otherwise besides the fact that some compilers act different than others, and I was curious how the Arduino IDE/compiler works, that' all.

It can help in coding 'n' stuff.

I've been sinking my head into a ton of datasheets for various STM32s looking for boards best suited to Bluetooth and Wi-Fi and general bs, plus the STM32Cube(+MX) IDE manual and Link V2 procedures and protocols and architectures and FPGA memory sectors and Ferroelectric Random Access Memory and assembler command lines and registers and buffers and...

...my mind is mush. 🤪 

Peace always! 🧐 


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 12 months ago
Posts: 452
2020-05-16 11:38 am  

FYI, I just checked my copy of C++ standard, and here is the official word on the matter:

C++ Order of Evaluation   Functions

Cheers!

 


ReplyQuote
bushgeek
(@bushgeek)
Active Member
Joined: 2 weeks ago
Posts: 5
2020-05-23 5:21 pm  

I had to try this out! I think my code is a valid test but let me know. I tried it on a hardware Arduino as well as an online C compiler, and wouldn't you know it, they produced different results!

The Arduino seems to process the arguments from right to left, but the C compiler (clang) seems to process them left to right.

Screen Shot 2020 05 23 at 11.07.32 PM
Screen Shot 2020 05 23 at 11.17.21 PM

ReplyQuote
starnovice
(@starnovice)
Estimable Member
Joined: 12 months ago
Posts: 159
2020-05-23 7:16 pm  

@bushgeek Shows two things: 1. @frogandtoad was right; 2. Don't write any code dependent on the order of the parameters.

Question: How does a person deal with interfacing to an assembly language routine if you don't know which order the parameters are passed?

Pat Wicker (Portland, OR, USA)


ReplyQuote
JoeLyddon
(@joelyddon)
Estimable Member
Joined: 9 months ago
Posts: 240
2020-05-23 7:33 pm  

@starnovice

Trial & Error?  🙂

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
starnovice
(@starnovice)
Estimable Member
Joined: 12 months ago
Posts: 159
2020-05-23 7:38 pm  

@joelyddon you could do that but then it wouldn't be portable to another compiler.

Pat Wicker (Portland, OR, USA)


ReplyQuote
JoeLyddon
(@joelyddon)
Estimable Member
Joined: 9 months ago
Posts: 240
2020-05-23 7:41 pm  

@starnovice

Punt!

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 12 months ago
Posts: 452
2020-05-25 8:47 am  

@bushgeek

It's interesting to see, but the Arduino does have it's own rules for the way some functions are implemented - It even says so for some of it's functions in it's reference pages.

Cheers for testing it out... that's a good way to learn!


ReplyQuote