Functions Return Values Parameters Getters and Setters Procedures

  • Slides: 22
Download presentation
Functions, Return Values, Parameters Getters and Setters

Functions, Return Values, Parameters Getters and Setters

Procedures in Everyday Life We use procedures all time – fire drill or recipe

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 in programming languages are the same They are set up in advance

Function 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

A Function by any other name Often referred to by different names Function procedure

A Function by any other name Often referred to by different names Function procedure Sub Stored procedure

Benefits of Functions Give greater functionality Hide complexity Promote code re-use Simplify maintenance

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

Functions gives Greater Functionality When we want to use the function we simply call it

Hides Complexity Who cares how it works?

Hides Complexity Who cares how it works?

Simplify Maintenance Create a function Call it multiple times around our system Need to

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

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

Functions are used to make… Event handlers Public methods in a class Public properties

Functions are used to make… Event handlers Public methods in a class Public properties in a class Constructors Test cases None of the above just a function

Function Example

Function Example

Return Values Two parts of the function work together…

Return Values Two parts of the function work together…

Pick the Return Data Type

Pick the Return Data Type

Calling Functions May use function with the assignment operator… When a function call is

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

Ignoring the Return Value

Void Functions

Void Functions

Parameters This code is not very versatile… Add parameters…

Parameters This code is not very versatile… Add parameters…

Use the Parameters

Use the Parameters

Calling the Function with Parameters

Calling the Function with Parameters

Public Functions to make Methods Public function in a class file Becomes a method

Public Functions to make Methods Public function in a class file Becomes a method in an object

Public Functions to make Properties - Getters and Setters

Public Functions to make Properties - Getters and Setters

Using the Debugger with Functions

Using the Debugger with Functions