Functions, Return Values, Parameters Getters and Setters
Procedures in Everyday Life We use procedures all time – fire drill or recipe They are set up in advance with a detailed set of instructions We only refer to them (Call them) when we need them Procedures allow us to repeat the same task over and over again
Function Procedures Procedures in programming languages are the same They are set up in advance with a detailed set of instructions We only refer to them (Call them) when we need them Procedures allow us to repeat the same task over and over again
Functions are used to make… Event handlers Public methods in a class Public properties in a class Constructors None of the above just a function
Function Example
Return Values Two parts of the function work together…
Pick the Return Data Type
Calling Functions May use function with the assignment operator… When a function call is completed the flow of code returns to the line where the call was made from!
Ignoring the Return Value
Void Functions
Parameters This code is not very versatile… Add parameters…
Use the Parameters
Calling the Function with Parameters
Benefits of Functions Give greater functionality Hide complexity Promote code re-use Simplify maintenance
Functions gives Greater Functionality When we want to use the function we simply call it
Hides Complexity Who cares how it works?
Simplify Maintenance Create a function Call it multiple times around our system Need to make a change Modify the one function definition All of the multiple calls benefit from the change
Functions Promote Code re-use A function should perform one task well Accept enough data via parameters for the task Return an appropriate value Organise into class libraries Allow us to recycle code
Public v Private Public function in a class file Becomes a method in an object
Functions and Public Properties Getters and Setters
Using the Debugger with Functions Use the sample driving test 2 as an example