You are on page 1of 13

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:
z That respond to a user input.
“ General (Sub) Subroutines:
z 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…
z Creating reusable blocks.

“ Also helpful for easy program maintenance.


z 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
z Example: button click, etc.
z Such methods are thus “called” by the event (not publicly available).
“ Objects can also have private methods (not for general use)…
z (more, later).

“ 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).
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
z 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
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.
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
z You should submit your entire project Folder.

z IMPORTANT: Name each submitted folder using your last name


and student ID number, separated by an underscore (_)…
z Example Folder: LastName_12345678_Task3_EA

“ Due Date:
“ By class-time on Tuesday, May 15 (2007).
“ Together, this Task and your upcoming RAD assignment will
count as your MIDTERM grade.

You might also like