Procedures Stefano Grazioli
Critical Thinking “Recording” error Extra credit (+1 point) to the first person who finds any material mistake in the vLabs or deck. Email timestamp will tell. Alternative ways of doing things do not count as errors. Easy meter $100 @500% x 10 year
Renaming a Project Unzip & make a copy of the project folder Look inside the folder. Find the folder that contains the .vbproj file and many other files. Keep that folder, delete the ‘outer’ stuff. Change the folder name to the new name. Click on .vbproj to start VS and change all other filenames from inside VS.
You do the talking Name, Major Learning objectives What you like about the class What can be improved Attitude towards the Tournament
Procedures Lasagne and Tagliatelle
Subs are a type of procedure Public Sub MyProcedure() Dim inputFromUser As String = "" Dim userNum As Double = 0 inputFromUser = InputBox("Please enter a number to square") userNum = Double.Parse(inputFromUser) userNum = userNum ^ 2 ShowTheUser(userNum) End Sub Private Sub ShowTheUser(someValue As Double) Range(“A1”).Value = "The result is " + someValue.ToString("N0") A procedure is a named sequence of steps Purpose: reuse & simplify existing code Good practice: shorter than a single screen Sometimes it takes arguments “Macro” is the old name for procedure
Local and global variables Dim userNum As Double = 0 Public Sub MyProcedure() Dim inputFromUser As String = "" inputFromUser = InputBox("Please enter a number to square") userNum = Double.Parse(inputFromUser) userNum = userNum ^ 2 ShowTheUser(userNum) End Sub Private Sub ShowTheUser(someValue As Double) Range(“A1”).Value = "The result is " + someValue.ToString("N0") Variables are names for physical places in your computer memory Variables have a lifecycle Variables have a scope Find the vars in here
Functions are another type of procedure input Return a single value Conceptually similar to the formulas in your worksheets output Private Function CalcInterest(r As Double, principal As Double, t As Double) As Double Dim interest As Double = 0 interest = principal * ((((1 + r) ^ t) - 1)) Return interest End Function
Argument Binding ‘ A function Sometimes difficult to understand for beginners ‘ A function Private Function CalcInterest(r As Double, principal As Double, t As Double) As Double … ‘ No binding myInt = CalcInterest(0.05, 1000, 10) ‘ With binding (typical use) myRate = 0.10 userPrincipal = 8000 years = 20 myInt = CalcInterest(myRate, userPrincipal, years)
Arguments vs. Globals Arguments are short-term variables used as input to procedures (Subs and functions) -best practice -more efficient & less errors Globals are variables defined outside of a Sub/Function -Use globals if variables have an extended life, are small, and are used by many procedures
WINIT What Is New In Technology?
Homework Using Subs & Functions