Presentation is loading. Please wait.

Presentation is loading. Please wait.

Method.

Similar presentations


Presentation on theme: "Method."— Presentation transcript:

1 Method

2 Outline What is Procedures
How to: Create a Procedure that Returns a Value How to: Return a Value from a Procedure How to: Call a Procedure That Returns a Value How to: Create and call a sub Procedure that don’t Returns a Value What is Procedure Overloading

3 procedures A procedure is a group of statements that together perform a task when called. After the procedure is executed, the control returns to the statement calling the procedure. Visual basic has two types of procedures: Functions Sub procedures or Subs Functions return a value, whereas Subs do not return a value. The term, method, describes a Sub or Function procedure that is accessed from outside its defining module, class, or structure.

4 Defining a Function A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code. Each time the procedure is called, its statements run, starting with the first executable statement after the Function statement and ending with the first End Function, Exit Function, or Return statement encountered. You can define a Function procedure in a module, class, or structure. It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

5 Declaration Function Syntax
The Function statement is used to declare the name, parameter and the body of a function. The syntax for the Function statement is: Where, Modifiers: specify the access level of the function; possible values are: Public, Private…etc FunctionName: indicates the name of the function ParameterList: specifies the list of the parameters ReturnType: specifies the data type of the variable the function returns [Modifiers] Function FunctionName [(ParameterList)] As ReturnType [Statements] End Function

6 Example Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Dim result As Integer If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result End Function

7 Function Returning a Value
In VB.Net, a function can return a value to the calling code in two ways: By using the return statement By assigning the value to the function name Function fun1(ByVal num As Integer) As Double Dim result As Integer result = num*2 return num End Function Function fun1(ByVal num As Integer) As Double Dim result As Integer result = num*2 Fun1 = result End Function

8 Call function You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The syntax for a call to a Function procedure is as follows: lvalue = functionname(argumentlist)  If ((functionname(argumentlist) / 3) <= expression) Then 

9 Calling example Module myfunctions
Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Dim result As Integer If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result End Function Sub Main() Dim a As Integer = 100 Dim b As Integer = 200 Dim res As Integer res = FindMax(a, b) Console.WriteLine("Max is : “+ res.tostring ) Console.ReadLine() End Sub End Module Output : Max is: 200

10 Defining a Sub Procedures
A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code. Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

11 Declaration Syntax The syntax for declaring a Sub procedure is as follows: The syntax for a call to a Sub procedure is as follows: [modifiers] Sub subname[(parameterlist)]  ' Statements of the Sub procedure.  End Sub  subname[(argumentlist)] 

12 Sub example Module Module1 Sub tellOperator(ByVal word As String) Console.WriteLine(word) End Sub Sub Main() Dim str As String = "ccsa227" tellOperator("hello") tellOperator(str) Console.ReadLine() End Module

13 Procedure Overloading
Overloading a procedure means defining it in multiple versions, using the same name but different parameter lists. The purpose of overloading is to define several closely related versions of a procedure without having to differentiate them by name. You do this by varying the parameter list.

14 Overloading Rules When you overload a procedure, the following rules apply: Same Name. Each overloaded version must use the same procedure name. Different Signature. Each overloaded version must differ from all other overloaded versions in at least one of the following respects: Number of parameters Order of the parameters Data types of the parameters

15 Overloading Example output
Sub cal(ByVal num1 As Integer, ByVal num2 As Integer) Dim result As Double result = num1 + num2 Console.WriteLine("Overloading 1: " + result.ToString) End Sub output Overloading 1: 6 Overloading 3: 8.5 Overloading 2: 14 Sub cal(ByVal num1 As Integer, ByVal num2 As Integer, ByVal num3 As Integer) Dim result As Double result = num1 + num2 Console.WriteLine("Overloading 2: " + result.ToString) End Sub Sub cal(ByVal num1 As Integer, ByVal num2 As Double) Dim result As Double result = num1 + num2 Console.WriteLine("Overloading 3: " + result.ToString) End Sub Sub Main() cal(2, 4) cal(3, 5.5) cal(4, 5, 5) Console.ReadLine() End Sub


Download ppt "Method."

Similar presentations


Ads by Google