You are on page 1of 15

*New Requirement: Define and use one user-defined function in your solution.

MidTerm Test
 Our Midterm Test will be on Tuesday
 Next Class, Oct 30.
 Format: Paper Test + Answer Sheet
 Mixture of Multiple Choice, Free Answer, etc

 Task 3 will also be Graded at this time:


 I will be look at your finished project during the Midterm Test
 You should come to class ON TIME on Tuesday…
 Place your ID card on your desk.
 Start your computer and Visual Studio .NET
 Load and Run your Project.
 You will be asked to place your keyboard on top of the computer.
 No computer use after the test is passed out.
 Students who do so will receive an F on the Midterm.

 I will come around to inspect your card/project…


 If you are not present, or your project is not loaded,
 A zero grade for the Task will be awarded.
Lecture 8: Methods
Outline
 In this lecture, we will discuss building Methods…
 Little mini-programs within our program…
 To do sub-tasks for us.
 Sometimes, methods are also called procedures.

 There are two types of Methods:


 Subroutines: Methods that do not return a value.
 Private Subroutines:
 That respond to a user input.
 General (Sub) Subroutines:
 Which can be called publicly.
 Functions: Methods that return a value.

 We will look at examples of both types.


Methods
 Now that we know some basics…
 Data classes: variables;
 The basics of process control: Conditional statements and Loops.

…We are set to discuss Methods.


 A Method is a self-contained block of code that ‘does something’.
 In other words, it performs its own isolated task.
 Can be thought of as a ‘mini-program’.
 Methods are thus very useful for modularizing our program:
 Organizing code into logical, independent tasks;
 This is helpful for Re-usability…
 Creating reusable blocks.

 Also helpful for easy program maintenance.


 Required changes to update a method’s behavior are made in the

method…
Types of Methods
 There are two basic types of Methods in VB .NET:
 Subroutines (both Private and ‘Sub’) –
 These do not return a value;
 Functions –
 These do return a value;
 A request to use a method is referred to as a ‘call’ to the method.

 Methods may also be classified by accessibility…


 Private – this means, ‘not for public use’
 Most private subroutines we will see are responding to an input event
 Example: button click, etc.
 Such methods are thus “called” by the event (not publicly available).
 Objects can also have private methods (not for general use)…
 (more, later).

 Public – this means, ‘for public use’


 Such methods typically are NOT responding to input events…
 They are called separately, and explicitly, in code (are publicly available).
Private Subroutines
 We have already been using private subroutines frequently.
 A private subroutine is a sequence of program code:
 From Private Sub to End Sub
 You should remember seeing these, in all of our examples.

 This kind of subroutine is usually responding to an event:


 A Button click
 A Label click
 The text in a TextBox is changed
 Etc…

 Note: Any Private Method cannot be called from other Objects.


 For our current purposes, this is the essential meaning of the keyword,
‘Private’
 In this class, we will not be calling Event Handlers from other Methods.
Example of an Event Subroutine
General (Sub) Subroutines
 A General (Sub) Subroutine is a sequence of program code:
 From Sub to End Sub
 Similar to a private subroutine.
 However, a ‘Sub’ subroutine is not responding to an event…
 Instead, it is directly called by us (we write it’s name in a line of code).
 In particular, ‘Sub’ subroutines are typically called from other Methods.
 Each time a ‘Sub’ subroutine is called:
 The statements within it are executed…
 i.e., statements after Keyword Sub,
 Until the matching End Sub is reached.

 Like private subroutines, ‘Sub’ Subroutines do not return a value.


 This makes them different from Functions (which do return a value).
Example of a Sub Subroutine
Functions
 A Function is a sequence of program code:
 From Function to End Function
 Somewhat similar to a Subroutine.
 But here, the Function keyword is used.

 Unlike subroutines, a Function returns a value.


 This means it executes all of its steps…
 Just like a Subroutine.
 However, it then Returns the result.
 This means, it sends the result back to the caller (= the calling line in the code).
 Functions provide re-usable mini-programs to perform calculations…
 Examples:
 evaluating data (e.g., grading);
 making multi-step calculations (e.g., estimating a the volume of a sphere);
 General data processing.
 Once defined, a Function can be called again and again.
 Just like Subroutines.
Internal Functions in VB .NET
 There are many internal (pre-defined) functions in VB
.NET
 In its standard library of functions.
 Some examples we have seen:
 x = Val( String s )
 Take input parameter string,
 Covert it to a numerical value (Double), and
 Return the numerical value for assignment to variable x.
 x = Format( Object o, String s )
 Take input parameter Object o (e.g., a Double or Integer),
 format it according to format string s (the second input parameter),
 and, return the formatted value for assignment to variable x.
 x = Int( Double d )
 Take input parameter d,
 Covert it to an Integer value, by truncation (chopping), and
 Return the resulting value for assignment to variable x.
User-defined Functions (Example)
*New Requirement: Define and use one user-defined function in your solution.
Midterm Test
 Our Midterm Test will be on Tuesday
 Next Class, Oct 30.
 Format: Paper Test + Answer Sheet
 Mixture of Multiple Choice, Free Answer, etc

 Task 3 will also be Graded at this time:


 I will be look at your finished project during the Midterm Test
 You should come to class ON TIME on Tuesday…
 Place your ID card on your desk.
 Start your computer and Visual Studio .NET
 Load and Run your Project.
 You will be asked to place your keyboard on top of the computer.
 No computer use after the test is passed out.
 Students who do so will receive an F on the Midterm.

 I will come around to inspect your card/project…


 If you are not present, or your project is not loaded,
 A zero grade for the Task will be awarded.

You might also like