Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.

Similar presentations


Presentation on theme: "PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within."— Presentation transcript:

1 PROGRAMMING Functions

2 Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within your programming.

3 What are functions? We have already used a lot of functions without actually realising that we are. These are things like LEN, InStr, Mid, Trim, UCase, Round A function is simply a routine (a piece of self-contained code) which is called as part of an expression and it brings back a value. A user-defined function is another type of procedure which we can create ourselves as part of the code of a project. It has the same kind of structure as a procedure. Unlike a procedure, it returns a value as a result of executing its code.

4 How a function works A function brings back a value. Dim numLetters as integer numLetters = 0 numLetters = Len(“Roger”) numLetters would now equal 5 LEN is a function. It was passed a value (Roger) and it counted the letters and brought back 5. This value was then stored in the variable numLetters.

5 Creating a Function Function Name() as datatype statement(s) End Function 1.Start with the keyword ‘Function’ 2.Give your function a name. 3.In the parenthesis, define any values which you would like to pass it. 4.Define what type of data it will pass back (integer, string, single etc). 5.Add your code statement(s) and a return value. 6.End the function with End Function.

6 Function Syntax Function MyFunc(strParam As String) As Integer Statement(s) MyFunc =… End Function MyFunc =… this is what is passed back to the calling procedure.

7 Simple Function Example Let’s step through an example where Procedures and Functions are used. ProcedureExample1

8 Objectives Review Understand the importance of modular programming. Know the role of functions within programming. Use functions within your programming.

9 When to use a function Use a function where you need a calculation or action carried out on data and a value is needed in the original program for further work. E.g. average calculation, total calculation, counting letters etc. Use a Procedure when you are performing more complex actions like opening files, populating an array, prompting for user input etc.

10 When to use a function Parameters and arguments in Functions ByVal You are just passing a copy of the value to the function/sub ByRef You are passing the actual variable itself so that the function can change the variable.

11 Plenary Each person in the class must explain one of their solutions for a question of their choice to someone else in the room. They should do this by stepping through the code in debug, explaining what happens as each line is executed.


Download ppt "PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within."

Similar presentations


Ads by Google