Is static more efficient than return?

i was trying to max optimize my codes, i was kind of struggling about how i should pass data through functions.

should i create a static variable and just kept write and read from that one variable?

or should i return the data to the loop function and pass it to another function?

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

I would guess from the nature of your question that you don't understand what declaring a variable as static actually does

Did you mean to ask whether you should decalre a variable as global rather than static (or both ) ?

1 Like

Like so many things it depends.

In small sketches, global variables (I dare to assume that's what you meant) can be convenient and easier.

As sketches grow, however, passing variables will make more sense and serve to help you contain things, as well as allowing you to use names freely without fear of code fusing confusing what variables you think you are taking about.

It also usually means it is easier to use one function in many places.

As for efficiency, the microprocessor is hella fast, and it is unlikely that you'll be doing anything where that would be the basis for choosing.

a7

1 Like

Learn the language and use the right design for the situation. Don't worry about efficiency, these processors are faster than the machines that used to run fortune 500 companies just a few years ago. BTW, it is NEVER codes, it is just CODE.

Sorry for the bit of confusion, i am a beginner that had started c++ for 4 months

I means global variables, im not sure what global variables are. (Maybe Static and object variables?)

I have been learning java for a few months and understand some basic codes, so i was not considering of using objective variables in c++ because i think i need to create object to use it?!

Hello ericneed10

You could use a search engine of your choice and search the WWW for ‘c++ +arduino’ to find a textbook that will give you the knowledge.

Do you not take hints?

I bet you are one of the people who say Legos and not Lego, to the displeasure of the Lego company its self.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.