*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. No computer use after the test is passed out. Students who do so will receive an F on the Midterm.

You will be asked to place your keyboard on top of the computer.
 

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. Which can be called publicly.

General (Sub) Subroutines:

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’. 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…

Methods are thus very useful for modularizing our program:
 

Types of Methods

There are two basic types of Methods in VB .NET:

Subroutines (both Private and ‘Sub’) –

These do not return a value; These do return a value;

Functions –

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). (more, later).

Objects can also have private methods (not for general use)…

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. A Button click A Label click The text in a TextBox is changed Etc…

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

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. Instead, it is directly called by us (we write it’s name in a line of code).

However, a ‘Sub’ subroutine is not responding to an event…

In particular, ‘Sub’ subroutines are typically called from other Methods. The statements within it are executed…

Each time a ‘Sub’ subroutine is called:

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. This means, it sends the result back to the caller (= the calling line in the code).

However, it then Returns the result.

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. Just like Subroutines.

Once defined, a Function can be called again and again.

Internal Functions in VB .NET

There are many internal (pre-defined) functions in VB .NET

In its standard library of functions. x = Val( String s )
  

Some examples we have seen:

Take input parameter string, Covert it to a numerical value (Double), and Return the numerical value for assignment to variable x. 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. Take input parameter d, Covert it to an Integer value, by truncation (chopping), and Return the resulting value for assignment to variable x.

x = Format( Object o, String s )
  

x = Int( Double d )
  

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. No computer use after the test is passed out. Students who do so will receive an F on the Midterm.

You will be asked to place your keyboard on top of the computer.
 

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.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.