Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about.

Similar presentations


Presentation on theme: "Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about."— Presentation transcript:

1 Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about in this chapter are subprocedures and functions. Both subprocedures (you’ll notice the keyword Sub at the beginning and ending of program blocks) and functions are known as procedures, which let you organize large programmable problems into smaller programming pieces.

2 Subprocedures and Functions In Visual Basic, the distinction between subprocedures and functions is minimal: Like subprocedures, functions are called with their names; however, they return a value to the calling statement. Functions have data types just as variables do. These data types are assigned to the function name and are used to denote the data type of the returning value. Assigning a value to the function’s name is the resulting returned value. This returning value can be assigned to a variable or used in a larger expression.

3 Subprocedures and Functions In short, procedures are simply void functions. So what’s a function, and what’s a void function? Whether you know it or not, you have already been using functions in this book. Simply remember the intrinsic Visual Basic functions Left(), Right(), Mid(), UCase(), and InStr(). Each of these functions takes a parameter as input and returns a value as output. Subprocedures, on the other hand, return no output or, in other words, are void of output. Basically, you will want to use functions when you need a value return to a calling statement and subprocedures when no return value is needed.

4 Adding Subprocedures 1.Editör içerisinde doğrudan yazarak 2.Tools * Add Procedure... The basic syntax for a subprogram is as follows: Public Sub Procedure_ Name() ‘Your code goes in here End Sub

5 Calling Subprocedures (Kullanılması) Private Sub Form_ Load() Call MyProcedure MyProcedure End Sub

6 Alt Yordamlarda Parametre Kullanımı Public Sub Add_Two_Numbers( x As Integer, y As Integer) Dim liResult As Integer liResult = x + y End Sub Private Sub Form_ Load() Call Add_Two_Numbers( 5, 3) Add_Two_Numbers 5, 3 End Sub

7 Exiting Subprocedures Public Sub Add_ Two_ Numbers( x As Integer, y As Integer) Dim liResult As Integer If x < 0 Or y < 0 Then Exit Sub End If liResult = x + y End Sub

8 Functions Visual Basic has many built-in or intrinsic functions.

9 Syntax for creating a Visual Basic function Public Function Function_Name( variable1 as DataType, variable2 as DataType,...) As DataType ‘Your code goes here End Function I do not use the keyword Sub to denote a subprogram; instead, I use the keyword Function. Functions can take many parameters as depicted in the example. the function itself is assigned a data type.

10 Fonksiyon Örneği Public Function Add( operand1 as Integer, operand2 as Integer) As Integer Add = operand1 + operand2 End Function After the two integers are added together, they are assigned to the name of the function, in this case Add. After that, the value of the added integers assigned to the name of the function is returned back to the calling procedure. Private Sub Command1_ Click() Dim result as Integer result = Add( 5, 15) End Sub

11 Exiting Functions Like subprocedures, functions can be exited from anywhere in the function before executing all function statements. To accomplish this, use the keywords Exit Function.

12 Interacting with the user Message Box Input Box

13 Message Box Message boxes are perfect when you want to alert the user to something happening in your program. For example, an error has occurred, or a game has just ended. Maybe you want to display a congratulatory note or a question about retrying some action.

14 MsgBox “Message”, Buttons, “Title Bar Caption” Örnek: MsgBox “My Message Box”,, “Chapter 5”

15 VB Button and Icon Types

16 MsgBox ile değişken kullanımı Dim lsMessageString as String lsMessageString = “Visual Basic Programming for the Absolute Beginner” &_ “by Michael Vine, 2001” MsgBox lsMessageString,, “Chapter 5”

17 Input Box Sometimes message boxes do not provide enough interaction between your program and the user. For example, what if you want to prompt the user for his or her name? Maybe you want to prompt a user to enter a starting level before a game begins. You can implement these types of scenarios with the Visual Basic input box.

18 Input Box InputBox( Prompt, [Title], [Default], [XPos], [YPos], [HelpFile], [Context]) Örnek: InputBox (“What is your name?”, “My Input Box Example”, “Your name goes here”)

19 Input Box Toplanan verinin bir değişkene ya da bir özelliğe atanması lblOutput. Caption = “Welcome “ & InputBox(“ What is your name?”, “My Input Box Example”, “Your name goes here”) Sayıları alırken dikkat! Dim liNumber As Integer liNumber = Val( InputBox(“ Enter a number between 1 and 10”, “A number Question”))


Download ppt "Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about."

Similar presentations


Ads by Google