Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters.

Similar presentations


Presentation on theme: "Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters."— Presentation transcript:

1 Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters v Control Arrays v Debugging

2 Lec6 P 2 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Modularity of Code: Procedures v As with C : Remove duplication of code within the same application Structured programming Well designed code procedures can be reused in other applications – Need to stand alone, The only link to the rest of the application is through passed parameters – Need to have a specific functionality

3 Lec6 P 3 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton v C uses only functions - used to return just one value or return more than one value via its parameters. Eg. Void addnums(float fnum1, float fnum2, float& fresult); Prototpye, two input and one output parameter addnums(2,5,fresult); Function Call void addnums(float fnum1, float fnum2, float& fresult) Procedure declaration, & - address of { fresult = fnum1 + fnum2; } v The function return the result via the parameter fresult

4 Lec6 P 4 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Question v Modify the example to show code to return the result via the functions name - ie. No output paraments to the function. Answer v Students to add the answer

5 Lec6 P 5 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton v VB uses Functions and Sub-Procedures (Subroutines) –Functions are used to return ONE value –Subroutines perform some action - also used to return more than one value –No prototypes are used in VB

6 Lec6 P 6 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Passing Parameters by Value & Reference v Visual Basic passes parameters by reference as default – This means that if you make a change to a parameter within the called sub-procedure the change is reflected in the original variable – It is also possible to pass parameters by value – This means that when you pass the parameter to the sub-procedure it takes a copy of it, leaving the original parameter unchanged – It is good practice to pass parameters by value when there is no intention of changing them.

7 Lec6 P 7 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Parameter Declarations v As in C you can declare parameters are either input (Value) or output (Variable/address of/by reference) v Value parameters are declared locally to the Procedure, hence used for input to a procedure. v Reference parameters share the same address as arguments and are used to return values from procedures (output). v Input : Value Students to add details v Output : Reference Students to add details

8 Lec6 P 8 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Modularity of Code: Sub-Procedures v Calling: Call subname(value1, value2, etc) or subname value1, value2, etc v Sub-Procedure Structure: Sub subname(value1 As type, Value2 As type, etc) declarations (local) statements End Sub Arguments Parameters

9 Lec6 P 9 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Modularity of Code: Functions v Passes back a value to the calling procedure v Calling – variable = funcname( value1, value2, etc) v Function structure: – Function funcname( value1 As type, value2 As type, etc) As type statements funcname = returnvalue End Function Note the return type

10 Lec6 P 10 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Sub-Procedure Locations v Where sub-procedures are located:

11 Lec6 P 11 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Adding a Sub-Procedure or Function -1 v Whilst you are in a code window, to add a new procedure into the general declarations section of either a form or a module, select view from the main menu bar v Then select New Procedure... from the sub-menu

12 Lec6 P 12 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton v This will bring up a dialog: v You should select the radio button for either a Sub-Procedure or a Function v You then need to enter the Sub-Procedure’s name

13 Lec6 P 13 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton  This adds a new sub-procedure or function structure definition to the code window Sub CubeNo () End Sub or Function CubeNo () End Function v You should then add any parameters required and a return type if it is a function

14 Lec6 P 14 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Adding Parameters  You can then add in the parameters and code to your function or procedure: Sub CubeNo (BYVAL lNum As Long, lCube As Long) lCube = lNum * lNum * lNum End Sub or Function CubeNo (BYVAL lNum As Long) As Long CubeNo = lNum * lNum * lNum End Function

15 Lec6 P 15 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Adding a Sub-Procedure or Function -2  Type in the Sub or Function and Visual Basic will automatically add the End Sub or End Function Sub CubeNo () End Sub or Function CubeNo () End Function v You should then add any parameters required and a return type if it is a function

16 Lec6 P 16 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Example v Let’s consider a simple program that works out the VAT that is payable on goods v You enter the price, click on Calc and the VAT and total price are calculated

17 Lec6 P 17 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Solution 1 : Sub-Procedures  Sub-Procedure CalcVAT: Sub CalcVAT (cValue As Currency, cVat As Currency) cVat = cValue *.175 End Sub  Calc Command Button: Sub Command1_Click () Dim cPrice As Currency Dim cVat As Currency cPrice = Val(Text1.Text) Call CalcVAT(cPrice, cVat) Label1.Caption = cVat Label2.Caption = cPrice + cVat End Sub Poor use of Parameters

18 Lec6 P 18 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Solution2 : Functions v We can declare CalcVAT as a function instead of a sub-procedure  Function CalcVAT: Function CalcVAT(cValue As Currency) As Currency CalcVAT = cValue *.175 End Function  Calc Command Button: Sub Command1_Click () Dim cPrice As Currency Dim cVat As Currency cPrice = Val(Text1.Text) cVat = CalcVAT(cPrice) Label1.Caption = cVat Label2.Caption = cPrice + cVat End Sub Again Poor use of parameters

19 Lec6 P 19 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Question v Poor use of parameter declaration in both solutions ! (a)Comment on the poor use of the Parameter declarations in both examples (b)Re-write the code using more suitable declarations.

20 Lec6 P 20 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Passing Arrays v eg Students to add example Note: We indicate that it is an array that is being passed to the procedure by putting () after the identifier name

21 Lec6 P 21 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Calling Procedures & Passing Arrays v Note the brackets after the array name used when an array variable is passed as an argument in the procedure call Students to add code

22 Lec6 P 22 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays v Visual Basic provides us with arrays of standard data types and user defined types : Dim inum(20) As Integer Dim studrec(100) As Studenttype v Visual Basic also provides us with arrays of Controls We can create an array of Command buttons or List boxes, etc This enables efficient processing of the controls

23 Lec6 P 23 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays 1 v Control Array is a number of controls that have the same event handler, the system passes the controls index into the procedure. 0 1 2

24 Lec6 P 24 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays 2 v The Control Array event handler can use a select statement instead of an if statement. 0 1 2

25 Lec6 P 25 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Control Arrays: Creating v Three ways to create a control array: – Copy an existing control and paste it onto form – Assign same name to more than one control – Set Index property of a control to non-null value For each of the above Visual Basic will ask do you wish to create a control array

26 Lec6 P 26 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton  Creating Control Arrays at run-time: Load ControlName(Index%) Unload ControlName(Index%) v Students to add notes on this section

27 Lec6 P 27 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Demo 1 - control arrays v Create a form with two text fields, a label to hold an answer and a control array of four command buttons. v When a button is pressed the operation & answer should be displayed in Label1: 45 - 52 = -7 or 3 + 5 = 4

28 Lec6 P 28 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Select Case Statement & Control Arrays v Using the Select Case statement to choose which control within a control array has been pressed: Sub Command1_Click (Index As Integer) Dim result As Integer Students to add code label1.caption = result End Sub

29 Lec6 P 29 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Demo 2 - Labels as a control array Create array of Labels at design time

30 Lec6 P 30 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Debugging - Introduction v There are three types of errors : v Compile errors occur as a result of code that is incorrectly constructed including syntax error –matched control structure, such as a Next statement without a corresponding For statement, –or programming mistakes that violate the rules of Basic, such as a misspelled word, a missing separator, – or a type mismatch. –syntax errors include passing an incorrect number of arguments to an intrinsic function –or forgetting to match parentheses

31 Lec6 P 31 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton Source Of errors v Run-time errors occur after the application starts to execute. –attempting an illegal operation, such as writing to a file that doesn't exist –or dividing by zero. v Logical errors the program doesn't perform as intended, and produces incorrect results.

32 Lec6 P 32 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton How to debug - overview v Debugging is a process by which you find and resolve errors in your code. v To debug code in Visual Basic, 1.Print the code, if you find it easier to read code on paper instead of onscreen. 2.Run the application to find trouble spots: –Run until an error stops execution, –or halt execution manually when you suspect an error by choosing Break

33 Lec6 P 33 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton 3.Use debugging tools and techniques to isolate bugs and to monitor code flow: – Set breakpoints to halt execution at certain points to look for problems such as u incorrect variable types, u mixups in variable names, u flaws in logical comparisons, u endless loops, u garbled output, u problems with arrays, and so on.

34 Lec6 P 34 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton –Add a watch expression at design time or while in break mode to allow you to monitor the value of a particular expression as your code runs. – Single step or procedure step through your code to trace through the application as it's running. – Use the Debug window to test individual lines of code or procedures, or to change values. – Enter break mode and choose Calls from the Debug window to see where your code is currently executing and trace the path showing how it got there.

35 Lec6 P 35 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton 4. Try out bug fixes and then make edits: –Test individual lines of new or debugged code in the Debug window. –Search and replace code for all occurrences of an error, checking other procedures, forms, or modules with related code.


Download ppt "Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters."

Similar presentations


Ads by Google