programming functions. objectives understand the importance of modular programming. know the role of...
TRANSCRIPT
![Page 1: PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/1.jpg)
PROGRAMMINGFunctions
![Page 2: PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022080905/56649e885503460f94b8ced3/html5/thumbnails/11.jpg)
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.