Notifications
Clear all

Let's start an argument about arguments!

18 Posts
8 Users
3 Likes
3,117 Views
itsCaseyDambit
(@itscaseydambit)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

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
Joined: 5 years ago
Posts: 1669
 

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)
Member
Joined: 5 years ago
Posts: 110
 

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)
Member
Joined: 5 years ago
Posts: 1458
 
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)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

@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)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

@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)
Member
Joined: 5 years ago
Posts: 1458
 

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)
Member
Joined: 4 years ago
Posts: 6
 

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)
Member
Joined: 5 years ago
Posts: 110
 

@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)
Member
Joined: 5 years ago
Posts: 157
 

@starnovice

Trial & Error?  🙂

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
(@starnovice)
Member
Joined: 5 years ago
Posts: 110
 

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

Pat Wicker (Portland, OR, USA)


   
ReplyQuote
JoeLyddon
(@joelyddon)
Member
Joined: 5 years ago
Posts: 157
 

@starnovice

Punt!

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@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
itsCaseyDambit
(@itscaseydambit)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

@bushgeek

Thank you so much for this test drive!

This really does show which argument goes through first, and it's as clear as a bell.

My apologies for taking so long to reply; my 'puter's hard drive crashed, and HP takes a while to create and ship new 'puters.

This was a really simple way of getting an answer to my question and I'm glad you took the time to get it straight. Well done, indeed.

Thanks again!


   
ReplyQuote
itsCaseyDambit
(@itscaseydambit)
Member
Joined: 4 years ago
Posts: 40
Topic starter  

@frogandtoad

I'm just naturally curious, and when I was writing a function call that had two separate function calls used as arguments within the first function call, the thought crossed my mind that in certain situations, the order in which the functions used as arguments are called could affect the outcome of calling function's return value. That sounds confusing AAF LOL - as I said, it depends on the situation. 

I try to keep my code as simple as possible because I'm too old for this.


   
frogandtoad reacted
ReplyQuote
Page 1 / 2