Visual Basic Function Procedures Topic Structure of the
Visual Basic Function Procedures
Topic & Structure of the lesson Function Procedure • Introduction • Function Procedures Visual Basic 2
Learning Outcomes Function Procedure At the end of this lecture you should be able to : 1. Write a Function Procedure 2. Use Exit Sub and Exit Function statements 3. Generate random numbers Visual Basic 3
Key Terms you must be able to use Function Procedure If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams: 1. Function 2. Exit 3. RND Visual Basic 4
Function Procedure A function is similar to a procedure and has the task of allowing modules to be built. It has the ability to return a integer value as the answer. Visual Basic 5
Function Format Function Procedure Private Function. Name(var 1 as type 1, var 2 as type 2, …. ) as datatype statement(s) Function. Name = expression End Function Visual Basic 6
Example Function Procedure Private Sub cmd. Convert_Click() label 1. caption = “” label 1. caption = Fto. C(Val(txt. Temp. F. Text)) End Sub Private Function Fto. C(t As integer) As integer Fto. C = (5 / 9) * (t - 32) End Function Visual Basic 7
Quick Review Question Function Procedure • Write a function to accept a radius from a textbox and display the area of a circle on a label • Formula : Area = pi * r • pi = 3. 142 Visual Basic 8
Solution Function Procedure Option explicit Const pi = 3. 142 Dim r as double Private sub comand 1_() r = val(inputbox (“Enter Radius”)) Msgbox areaofcircle(r) End sub Private function areaofcircle(radius as double) as double areaofcircle = pi * radius End Function Visual Basic 9
Quick Review Question Function Procedure Write an program to determine the volume of a sphere using a Function. Accept the radius from a textbox. Display the volume on a label. Volume of a sphere : 4/3 * pi * r 3 Note : Function volume() Visual Basic 10
Public/Private Variables Function Procedure A Public variable is a variable whose scope or lifespan is throughout the whole program. This means that all sub procedures (or functions) can make use of it. Visual Basic 11
Public Variables Function Procedure A Module is where global variables and sub procedures can be declared Visual Basic 12
Code Module –Form 1 Function Procedure Visual Basic 13
Code Module – Form 2 Function Procedure Visual Basic 14
Module Function Procedure Visual Basic 15
EXIT FUNCTION Function Procedure • EXIT SUB will alter the flow of control to your program • Executing EXIT SUB in a sub procedure causes an immediate exit from that procedure. Visual Basic 16
EXIT SUB Function Procedure Private Sub cmd. Begin_Click() Dim x As Integer For x = 5 To -1 Step -1 Call Print. Numbers(x) Next x cmd. Begin. Enabled = False End Sub Visual Basic 17
EXIT SUB Function Procedure Private Sub Print. Numbers(number As Integer) If number >=0 Then Print number Else Print "Exiting Sub with number = " & number Exit Sub End If End Sub Visual Basic 18
Exit Function Procedure • Executing Exit Function causes an immediate exit from the function • Control is returned to the caller and the next statement in sequence after the call is executed Visual Basic 19
Exit Function Private sub cmddivide_click() Dim numerator , denominator as integer Dim result as string Numerator = val(txtnum. text) Denominator = val(txtden. text) Result = divide(numerator, denominator) If result = “” then msgbox “Divide Zero attempted” Else msgbox result End if End sub Visual Basic Function Procedure 20
Exit Function Procedure Private function divide(n as integer, d as integer) as string If d = 0 then exit function Else divide = “Division yields” & n/d End if End Function Visual Basic 21
Random Number Generation Function Procedure • RND function returns a random number in a range • Dieface = 1 + Int(6 * rnd()) to generate random numbers from 1 to 6 Visual Basic 22
Exercise Function Procedure • Write a program that will generate a random number from 1 to 100 when a command button is pressed. Visual Basic 23
Event Procedures Function Procedure One of the main advantage in most of the 4 GL’s like VB, provide the event procedure. Event Procedure : Will automatically executed once the event occurs. Sample VB Events: click Key. Press Mouse. Down Dragdrop Key. Down Mouse. Up Dragover Key. Up Mouse. Move Got. Focus Lost. Focus Visual Basic 24
Follow Up Assignment Function Procedure • Write a program that uses a function called Circle. Area to calculate and print the area of a circle. The user should input the radius in a Text. Box and display the answer using a Msg. Box. Formula Area of Circle = pi * r Visual Basic 25
Follow Up Assignment Function Procedure Write a Function procedure that takes two String arguments representing a first name and last name, concatenates the two Strings to form a new String representing the full name and returns the concatenated String. Visual Basic 26
Follow Up Assignment Function Procedure Write a program that Inputs an Integers and passes them to function Is. Even, which uses the Modulus operator to determine if an Integer is even. The procedure should take an Integer argument and return True if the Integer is even and False otherwise. Visual Basic 27
Summary of Main Teaching Points Function Procedure • Function Procedures • Used to solve an equation • Can return a single value as the answer Visual Basic 28
Question and Answer Session Function Procedure Q&A Visual Basic 29
- Slides: 29