← Back to Book Detail

2 Instructions, Functions, and Frameworks (1/24) -- Rick's Measurement for Mechatronics Note...

Browse
4%

2 Instructions, Functions, and Frameworks

2 Instructions, Functions, and Frameworks A program is a list of instructions, executed in order from top to bottom. You can usually think of each line of code as a separate instruction, like an assignment statement that calculates a value and stores the result in memory as the value of a variable. Conditional constructions like if / then / else allow us to execute different lists of instructions based on values already calculated and iteration constructions like for loops allow us to repeat a set of instructions multiple times. Functions allow us to group sets of instructions together so that we can call them from multiple points in a program to perform the same operations on different sets of values. Functions As in algebra, functions in programming take some arguments and return a value. defines the function and shows how to apply it to any argument. The variable is just a place holder. In Python we could say the same thing with def f(x): # define f(x) as all the indented stuff after the : a = x**2 # first term is assigned to a a = a + 2 * x # add on the second term a = a + 1 # and then the third return a # return the value to the calling expression so that any time we called the function in an expression like y = 5 + f(27 + z) we will get back a value calculated according to the statements in the function definition f( ) . The value 27+z will be evaluated and passed to the function, where that value will be used in place of the variable x . We will get into a little more detail about variables and number format in the following chapters. Functions can be as simple as this straight through calculation, or much more complex with multiple decisions to make and instructions that may be doing much more than just calculations. All functions have three things in common: - They take some number of arguments (including the special case where that number is zero) - They execute some instructions to achieve a goal (including the special case where they do nothing) - They return something as a result when they are finished (including the special case where that something is just the null set, nothing, or “ void ” in C) It’s possible, but not very interesting for a function to take 0 arguments, execute 0 statements, and/or return an empty result. Before defining a function in code, you need to decide what inputs it needs as its arguments, what it will do before returning, and what output it will return. Calling Functions A function can be called as part of an expression in an assignment statement like this y = 5 + f(27 + z) and the result returned by the function will be used in the expression to calculate the result eventually assigned to y , or a function can simply be called on its own as a standalone statement f(27 + z) When called this way, the return value, if any, will be discarded after the function completes. This is only useful if the code in the function is doing something besides just calculating a return value. A function could also be used a
← Previous Chapter Next Chapter →