Upcoming SlideShare
×

# Unit 6: Functions and Subroutines

619 views
561 views

Published on

Published in: Education
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
619
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
19
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Unit 6: Functions and Subroutines

1. 1. Excel Macros Level 1<br />Functions and Subroutines<br />
2. 2. Functions & Subroutines<br />Functions:<br />Return a value<br />Subroutines<br />Do not return a value<br />Both are used to:<br />Make code modular (i.e. reusable)<br />Make code more readable<br />4/29/2010<br />M. Campbell - 2010<br />2<br />p. 69<br />
3. 3. Activities<br />In the Unit 6 Activities complete:<br />Activity 1: Uppercase Subroutine<br />Activity 2: Lowercase Subroutine<br />Activity 3: Title Case Subroutine<br />Activity 4: Uppercase Function<br />Note that these can all go in the same module<br />4/29/2010<br />M. Campbell - 2010<br />3<br />
4. 4. Functions<br />In Activity 4 you saw the function declaration:<br />Public Function Uppercase2(Text As String) _<br /> As String<br />4/29/2010<br />M. Campbell - 2010<br />4<br />
5. 5. Functions<br />In Activity 4 you saw the function declaration:<br />Public Function Uppercase2(Text As String) _<br /> As String<br />4/29/2010<br />M. Campbell - 2010<br />5<br />p. 76<br />Indicates the Function's scope:<br />Public:<br /><ul><li>Can be called from any module</li></ul>Private:<br /><ul><li>Can only be called from module it was declared in</li></li></ul><li>Functions<br />In Activity 4 you saw the function declaration:<br />Public Function Uppercase2(Text As String) _<br /> As String<br />4/29/2010<br />M. Campbell - 2010<br />6<br />p. 69<br />Declares this code to be a Function<br />
6. 6. Functions<br />In Activity 4 you saw the function declaration:<br />Public Function Uppercase2(Text As String) _<br /> As String<br />4/29/2010<br />M. Campbell - 2010<br />7<br />p. 69<br />The Function's name<br />This is something that you define<br />
7. 7. Functions<br />In Activity 4 you saw the function declaration:<br />Public Function Uppercase2(Text As String) _<br /> As String<br />4/29/2010<br />M. Campbell - 2010<br />8<br />p. 71<br />This is the Function's Parameter list<br />Value is a Parameter<br />When you ran the Function, the value of Text was set to hello<br />The value placed in a Parameter is known as an Argument<br />
8. 8. Functions<br />In Activity 4 you saw the function declaration:<br />Public Function Uppercase2(Text As String) _ As String<br />4/29/2010<br />M. Campbell - 2010<br />9<br />p. 69<br />This is the Function's Return Type<br />In this Function, it will return a value of type String<br />
9. 9. Functions<br />In Activity 4 you saw this line of code:<br />Uppercase2 = UCase(Text)<br />4/29/2010<br />M. Campbell - 2010<br />10<br />
10. 10. Functions<br />In Activity 4 you saw this line of code:<br />Uppercase2 = UCase(Text)<br />4/29/2010<br />M. Campbell - 2010<br />11<br />This is the Function's Return statement<br />The value of the expression on the right side of the assignment operator will be returned by the Function<br />
11. 11. Functions<br />In Activity 4 you saw this line of code:<br />Uppercase2 = UCase(Text)<br />4/29/2010<br />M. Campbell - 2010<br />12<br />Remember that Text is the parameter to the Function.<br />It currently holds the value of hello<br />This sends the value to the UCase Function which converts hello to uppercase<br />
12. 12. Subroutines<br />In Activity 1 you saw the subroutine declaration:<br />Sub Uppercase()<br />4/29/2010<br />M. Campbell - 2010<br />13<br />
13. 13. Subroutines<br />In Activity 1 you saw the subroutine declaration:<br />Sub Uppercase()<br />4/29/2010<br />M. Campbell - 2010<br />14<br />p. 70<br />Declares this code to be a Subroutine<br />
14. 14. Subroutines<br />In Activity 1 you saw the subroutine declaration:<br />Sub Uppercase()<br />4/29/2010<br />M. Campbell - 2010<br />15<br />p. 70<br />The Subroutine's name<br />This is something that you define<br />
15. 15. Subroutines<br />In Activity 1 you saw the subroutine declaration:<br />Sub Uppercase()<br />4/29/2010<br />M. Campbell - 2010<br />16<br />p. 76<br />Note the absence of the scope keyword Public or Private<br />If it is omitted, the procedure (subroutine or function) is declared to be Public<br />It should always be included for readability<br />
16. 16. Calling Subroutines<br />Has the form:<br />[Public or Private] Sub SubroutineName (_<br />Param1 As DataType1, Param2 As DataType2,_ …)<br />To call:<br />Call SubroutineName(parameters, …)<br />Or<br />SubroutineNameparameters, …<br />4/29/2010<br />M. Campbell - 2010<br />17<br />p. 70<br />
17. 17. Activities<br />In the Unit 6 Activities complete:<br />Activity 5: Calling a Function with a Subroutine<br />4/29/2010<br />M. Campbell - 2010<br />18<br />
18. 18. Optional Arguments<br />Can set some arguments to be optional<br />SubDisplayName(firstNameAs String, _<br />OptionallastNameAs String, _<br /> Optional midName As String)<br />Note that all optional arguments must come at end of parameter list<br />4/29/2010<br />M. Campbell - 2010<br />19<br />p. 71<br />
19. 19. Optional Arguments<br />SubDisplayName(firstNameAs String, _<br />OptionallastNameAs String, _<br /> Optional midName As String)<br />To call procedure with only firstName and midName:<br />CallDisplayName("Winnie", , "the")<br />Must include blank spot as arguments expected in order defined in parameter list<br />4/29/2010<br />M. Campbell - 2010<br />20<br />p. 71<br />
20. 20. Positional Arguments<br />In previous example, we used positional arguments:<br />CallDisplayName("Winnie", , "the")<br />The position of arguments tells VBA which parameters they fill<br />4/29/2010<br />M. Campbell - 2010<br />21<br />p. 73<br />
21. 21. Named Arguments<br />Naming the arguments:<br />Improves readability<br />Removes the need for blank spaces<br />Allows arguments to go in any order<br />CallDisplayName(midName:= "the", _<br />firstName:= Winnie")<br />4/29/2010<br />M. Campbell - 2010<br />22<br />p. 73<br />
22. 22. Activities<br />In the Unit 6 Activities complete:<br />Activity 6: Optional and Named Arguments<br />4/29/2010<br />M. Campbell - 2010<br />23<br />
23. 23. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRefiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />24<br />p. 73<br />
24. 24. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRefiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />25<br />p. 73<br />Initializes x to 5<br />
25. 25. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRefiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />26<br />p. 73<br />Displays 5<br />
26. 26. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRefiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />27<br />p. 73<br />Calls AddOne and sends x<br />
27. 27. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRefiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />28<br />p. 73<br />AddOne is sent a reference to x, effectively replacing the i with x<br />
28. 28. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRef x As Integer)<br /> x = x + 1<br />End Sub<br />M. Campbell - 2010<br />29<br />p. 73<br />AddOne is sent a reference to x, effectively replacing the i with x<br />
29. 29. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRef x As Integer)<br /> x = x + 1<br />End Sub<br />M. Campbell - 2010<br />30<br />p. 73<br />1 is added to x to get 6<br />This is the same x as in ProcedureA<br />