Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sub Procedures and Functions

Similar presentations


Presentation on theme: "Sub Procedures and Functions"— Presentation transcript:

1 Sub Procedures and Functions
Visual Basic Sub Procedures and Functions

2 Topic & Structure of the lesson
Introduction to Modular Design Concepts Write Sub Procedures Compare between Call by Value and Call by Reference

3 Write a sub program and pass arguments to it
Learning Outcomes At the end of this lecture you should be able to : Declare a sub program Write a sub program and pass arguments to it Write a Call-By-Value and Call By Reference sub procedure

4 Key Terms you must be able to use
If you have mastered this topic, you should be able to use the following terms correctly in your assignments and tests: Call By Ref By Val

5 Modular Design Concepts
Modularity refers to breaking a large problem down into smaller self-contained modules. This allows the programmer to isolate problems within the program by looking at specific areas. Without Breaking problems down into smaller modules will increase program maintenance cost.

6 Dividing code into procedures
Divide & Conquer Approach Makes program development more manageable. Software Reusability Using existing procedures as building blocks for new programs Avoid Duplicating programs

7 Different Modular Techniques
Visual Basic provides us with the ability to represent modules through the use of the following:- Sub Procedures Function Procedures Event Procedures

8 Sub Procedures What are Sub Procedures?
Sub Procedures are subprograms or routines which are self contained and are used to perform a particular task.

9 Writing a Sub Procedure
Private Sub cmdAdd_click() Call add(2,1) End sub Private sub add(num1,num2 as integer) Dim total as integer total = num1 + num2 MsgBox total End Sub Arguments parameters

10 Quick Review Question Write a program to accept two numbers using textboxes and pass the numbers into a subprogram called product to multiply them. Display the answer in the product procedure. Use a Msgbox to display the answer

11 Answer Private Sub cmdmultiply_Click() Dim a,b as integer
Call product (a, b) End Sub Private Sub product (num1 As integer, num2 As integer) Msgbox ("The product of “ & num1 & "and“ & num2 & "is“ & num1 * num2)

12 No Arguments – No return value
MAIN PROGRAM Private Sub Command1_Click( ) ‘ Check the parenthesis. There is no argument Call No_arg_No_return End Sub No arguments

13 No Arguments – No return value
SUB PROCEDURE Public Sub No_arg_No_return() Dim x, y, z As Integer x = 20 y = 30 z = x * y Label1.Caption = "TOTAL = " & z End Sub

14 OUTPUT

15 Quick Review Question Write a program that will accept three numbers and pass the numbers to a procedure minimum to determine the smallest number. Accept the three numbers using input box and display the smallest number on a text box.

16 Solution Private Sub cmdSmallest_Click() Dim value1 As Long, value2 As Long, value3 As Long value1 = txtOne.Text value2 = txtTwo.Text value3 = txtThree.Text Call Minimum(value1, value2, value3) End Sub Private Sub Minimum(min As Long, y As Long, z As Long) If y < min Then min = y End If If z < min Then min = z lblSmallest.Caption = "Smallest value is " & min

17 Call By Reference When you pass a variable to a sub procedure and return its updated value By default Call By Reference is used in VB

18 Call By Reference - Procedure
Private sub a() dim a as integer a = 5 call square(a) print a end sub private sub square(num as integer) num = num * num Ans a = 25

19 Call By Value When you pass a variable to a sub program and it does not affect the value from the main program

20 Call By Value Private sub a() dim a as integer a = 5 call square(a)
print a end sub private sub square (byval num as integer) num = num * num Ans a = 5

21 Group Exercise Write a program that accepts a student mark and passes the mark to a subprogram called grade which will determine the students grade. Mark Grade A B C D 0 – 39 F

22 Follow Up Assignment Write a program that uses a Sub procedure Maximum to determine the largest of three Integers. The smallest value is displayed in a Label.

23 String Functions The Len Function
The Len function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The syntax is Len (Phrase) For example, Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22 The Len function can also return the number of digits or memory locations of a number that is stored in the computer. For example, X=sqr (16) Y=1234 Z#=10# Then Len(x)=1, Len(y)=4, and Len (z)=8

24 String Functions The Right Function
The Right function extracts a substring from a phrase, starting from the Right. The syntax is Right (Phrase, n) where n indicates the number of characters that you wish to extract starting from the right-most character.  For example,  Right(“Visual Basic”, 4) = asic The Left Function The Left function extracts a substring from a phrase, starting from the left. The syntax is Left(Phrase, n) where n indicates the number of characters that you wish to extract starting from the left-most character..  For example,  Left (“Visual Basic”, 4) = Visu

25 String Functions The Ltrim Function
The Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is Ltrim(Phrase) .For example,  Ltrim (“  Visual Basic”, 4)= Visual basic Rtrim Trim Ucase and the Lcase

26 String Functions The Mid Function
The Mid function extracts a substring from the original phrase or string. The syntax is: Mid(phrase, position, n) Where position is the starting position of the phrase from which the extraction process will start and n is the number of characters to be extracted. For example, Mid(“Visual Basic”, 3, 6) = ual Bas The InStr function The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is Instr (n, original phase, embedded phrase) Where n is the position where the Instr function will begin to look for the embedded phrase. For example Instr(1, “Visual Basic”,” Basic”)=8

27 String Functions The Rnd Function
Rnd is is very useful function for dealing with the concept of chance and probability. The Rnd function returns a random value between 0 and 1. The Numeric Functions The numeric functions are Int, Sqr, Abs, Exp, Fix, Round and Log. a) Int is the function that converts a number into an integer by truncating its decimal part and the resulting integer is the largest integer that is smaller than the number. For example, Int(2.4)=2, Int(4.8)=4, Int(-4.6)= -5, Int(0.032)=0 and so on. b) Sqr is the function that computes the square root of a number. For example, Sqr(4)=2, Sqr(9)=2 and etc. c) Abs is the function that returns the absolute value of a number. So Abs(-8) = 8 and Abs(8)= 8.

28 Numeric Functions d) Exp of a number x is the value of ex. For example, Exp(1)=e1 = e) Fix and Int are the same if the number is a positive number as both truncate the decimal part of the number and return an integer. However, when the number is negative, it will return the smallest integer that is larger than the number. For example, Fix(-6.34)= -6 while Int(-6.34)=-7. f) Round is the function that rounds up a number to a certain number of decimal places. The Format is Round (n, m) which means to round a number n to m decimal places. For example, Round (7.2567, 2) =7.26 g) Log is the function that returns the natural Logarithm of a number. For example, Log 10=


Download ppt "Sub Procedures and Functions"

Similar presentations


Ads by Google