programming functions. objectives understand the importance of modular programming. know the role of...

11
PROGRAMMING Functions

Upload: frank-white

Post on 30-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

PROGRAMMINGFunctions

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

Objectives• Understand the importance of modular programming.

• Know the role of functions within programming.

• Use functions within your programming.

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

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.

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

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.

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

Creating a FunctionFunction 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.

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

Function SyntaxFunction MyFunc(strParam As String) As Integer

Statement(s) MyFunc =…

End Function

MyFunc =… this is what is passed back to the calling procedure.

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

Simple Function Example• Let’s step through an example where Procedures and

Functions are used.

• ProcedureExample1

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

Objectives Review• Understand the importance of modular programming.

• Know the role of functions within programming.

• Use functions within your programming.

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

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.

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

When to use a function• Parameters and arguments in Functions

• ByValYou are just passing a copy of the value to the function/sub

• ByRefYou are passing the actual variable itself so that the function can

change the variable.

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

PlenaryEach 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.