0

For what we use functions

about functions

24th Jul 2017, 3:08 PM
Hussein Adam
Hussein Adam - avatar
2 Answers
+ 2
Functions map (convert) inputs to new outputs. An example function call: "TimesTwo" print( TimesTwo(3) ) --> outputs 6 print( TimesTwo(14) ) --> outputs 28 Output is often deterministic: This means the same inputs always produce the same outputs (though they might be relative instead of absolute). Functions can also do other tasks; not everything has to return a value (work) that way. For example, a function could move a motor "ahead 5 steps" regardless of position, then turn on a light. Instead of coding a long set of explicit instructions for motor control, you could just call functions (move forward 10, left 5, up 7). Each function calls a hidden motor control function, and you don't need to know how that works. Not "needing to know" is a benefit of "abstraction" (the details aren't important to you, but the work is). With enough 'abstractions'...you could say you have reusable code.
24th Jul 2017, 3:35 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
To reuse the same block of code in multiple places
24th Jul 2017, 3:22 PM
Bogdan Sass
Bogdan Sass - avatar