← Back to Book Detail

4 Variables, Assignment, and Expressions (4/24) -- Rick's Measurement for Mechatronics Note...

Browse
16%

4 Variables, Assignment, and Expressions

4 Variables, Assignment, and Expressions As in algebra, we assign symbolic names to the numbers and other elements we want to manipulate. In algebra is a statement about the relationship between and . We could also invert it to get the two values of that correspond to a particular value of . In programming y = x * x + 2 * x + 1 is an assignment statement giving the instruction to start with the value stored at location x , multiply it by itself, add the product of the original value and 2, add 1 to the total, and then store the result in the location referred to symbolically as y . The order in which things happens may depend on the language, although it will usually follow the expected order for addition, subtraction, multiplication and division. Use parentheses when necessary to make the order of operations unambiguous to both people and computers. Kernighan and Ritchie wrote that “The moral is that writing code that depends on order of evaluation is a bad programming practice in any language. Naturally, it is necessary to know what things to avoid, but if you don’t know how they are done on various machines, you won’t be tempted to take advantage of a particular implementation.” The same goes for order of evaluation differences between languages: being explicit decreases the chances of errors and increases the readability of your code for humans. Defining and Initializing Variables Defining a variable can only happen once, giving the variable a unique name, and allocating some storage space to save the value(s) associated with the variable. int i; // defines the variable i, could be global or local to a function i = 5; // assigns 5 to variable i, can only happen inside a function is C code that defines the variable i as having type int, assigns a block of memory (either 2 or 4 bytes) large enough to hold an integer, then puts the integer value 5 in that storage space in memory. The same could be accomplished in the combined statement: int i = 5; // could be global or local to a function Always initialize your variables to avoid unexpected results. In Python or MATLAB this is simple: variables are defined automatically when you assign something to them and the type is set according to what you assigned to them. Get in the habit of writing real numbers with decimal points every time, e.g. 42.0 as it will never be wrong and will sometimes be important. Be careful with the scope of your variables. A variable defined outside of a function is global and is available everywhere in your program code. A variable defined inside a function is local, and is available only inside that function, vanishing when the function finishes execution. Memory for local variables is only allocated when a function starts executing and the variables are trashed when the function finishes. The memory allocation may be in a different location each time, leading to potentially strange results if you don’t initialize every time. If a local variable has the same name as a gl
← Previous Chapter Next Chapter →