This action might not be possible to undo. Are you sure you want to continue?
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.
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.
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;
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)…
Public – this means, ‘for public use’
This type of methods do NOT respond to input events. They are called separately, and explicitly, in code (are publicly available).
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: Event Handlers cannot be called from other Methods…
Also, any Method labeled Private cannot be called from other Objects. For our current purposes, this is the essential meaning of the keyword, ‘Private’
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 may be 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 the different from functions (which do return a value).
Example of a Sub Subroutine
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…
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.
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 (i.e., 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.
Task 3 (Pyth. Triples) will be Graded
Submission Method: Electronic via APU Net
You can find instructions for submission in the APU Net Guide (p. 85).
Use Internet Explorer 6 to submit (not Netscape) Place your competed task in: ReportSubmit > jarose > Introduction to Programming EA > Task_3 You should submit your entire project Folder. IMPORTANT: Name each submitted folder using your last name and student ID number, separated by an underscore (_)…
Example Folder: LastName_12345678_Task3_EA
By class-time on Tuesday, May 15 (2007). Together, this Task and your upcoming RAD assignment will count as your MIDTERM grade.