LATEST UPDATES

Search This Blog

Chapter 4: Arrays and Procedures

Tuesday, September 3, 2013

Chapter 4: Arrays and Procedures
Array is the sequence of memory location that is used to hold same type of data. So an array is a group of homogenous elements accessed under a common name.
One dimensional array
An array with one subscripted variable is called one dimensional array. Syntax for declaring an array is as follows
Dim arrayname (size) as datatype
Eg: -
Dim age (5) as integer
          It can hold 6 ages of persons. The first element is represented by using the subscript age (0), second is age (1) and last is age (5).
          To input, process and output the elements in an array a loop statement is must.
Basic operations on arrays
Array traversal: - Visiting and processing the elements in an array is called traversal. Linear search: - Process of searching sequentially the element in an array is called lnear search.
Example program for traversal
Code for finding sum and percentage of marks of 6 subjects
Private sub command1_click()
Dim i as integer, sum as integer, percent as single
Dim mark(6) as integer

For i= 1 to 6
Mark(i)= inputbox “Enter marks” &i
Next i

Sum=0
For i= 1 to 6
Sum= sum+mark(i)
Next i

Percent=sum/3
Text1.text=sum
Text2.text=percent
End sub
Procedures
Every VB application is made up of small, self contained segments or group of codes is called procedures. It allows dividing a big program into small units. This approach is called modular approach. It enhances the execution speed and can easy to debug.
          So procedure contains a collection of instructions that can solve a specified task. It may be defined by the user or compiler. So procedures are divided into two. They are Subroutines and functions.
Subroutines
          A subroutine is a group of instructions that have a specific name and can carries out a well defined task. They place in between ‘Sub’ and ‘End Sub’ commands. Consider the code given below.
Sub simpleinterest( )
Dim P as Integer, N as Integer, R as Integer, SI as double
          P=5000
          N=3
          R=10
          SI=P*N*R/100
          Msgbox “Simple interest=” &SI
End Sub
The above code is a subroutine to find the simple interest for the given values. Here the subroutine name is ‘Simple interest’.
The above subroutine can be use by calling it. The code used to call a subroutine is given below when we click on a command button.
          Private sub command1_click( )
                   Simpleinterest
          End Sub
The values are passed to a subroutine is called ‘Arguments’. The above subroutine is use only for the given values. i.e. 5000, 3 and 10. To overcome this situation we can use the concept of arguments. For example consider the code given below.
Sub simpleinterest(P as Integer, N as Integer, R as Integer)
                   Dim SI as double
          SI=P*N*R/100
          Msgbox “Simple interest=” &SI
End Sub
When we call the subroutine we can pass values to it. They are called arguments.
Private sub command1_click( )
                   Simpleinterest 10000, 4, 7
End Sub
Here the subroutine works with the values that are passed with the calling statements.
Example: Write a program to find the factors of a given number by using subroutine.
Sub factors(num as integer)
          Dim i as inieger
                   For i=2 to num/2
                             If num mod i=0 then
                                      Msgbox i
                             End If
                   Next i
End Sub

We can call the above subroutine as
Private sub command1_click( )
Factors 50         
End Sub
By modifying the above code we can accept the values from a text box.
Private sub command1_click( )
          Dim Value as integer
Value = Text1.text
Factors Value            
End Sub
Argument passing mechanism
To a subroutine arguments can be passing in two ways.
1.                Passing arguments By Reference
2.                Passing arguments By Value
Passing arguments By Reference
The key word ‘Byref’ is used to pass the arguments to the subroutine.
Example: Write a program to swap (Inter change the values) two integers using subroutine.
Sub swap(Byref a as integer, Byref  b as integer)
          Dim t as integer
          t=a
a=b
b=t
End Sub
We can call the subroutine by the given code.
Private sub command1_click( )
Dim p as integer, q as integer
p=20
q=10
swap p,q
msgbox p, q
End Sub
In pass by reference method we actually pass the variable itself. Any changes made to the argument by the procedure will affect the variable in calling program.
Passing arguments By Value
The second method is called passing arguments by value. Also we can use the key word ‘Byval’ with the function. The default mechanism is passing by reference.
Example: Write a program to swap (Inter change the values) two integers using subroutine.
Sub swap(Byval a as integer, Byref  b as integer)
          Dim t as integer
          t=a
a=b
b=t
End Sub
We can call the subroutine by the given code.
Private sub command1_click( )
Dim p as integer, q as integer
p=20
q=10
swap p,q
msgbox p, q
End Sub
In pass by value method we actually copy the variable itself. Any changes made to the argument by the procedure would not affect the variable in calling program.
In these two if we want to change the values of calling and called program then the call by reference method is better. If we would not change these values then call by value is better because it saves memory.
Functions
They are built in functions defined by the VB compiler. It returns a value also.
They are classified into five.
1.    Math functions (abs, sqr, round, int)
2.    String functions (len, lcase, ucase, ltrim, rtrim, trim, left, right, mid, instr)
3.    Date function (date, now, time, weekday, month, day, year, datediff)
4.    Conversion function (val, str, asc, chr, format)
5.    Miscellaneous function (line, circle, rgb, rnd)
Math functions
abs( )
          It returns the absolute value of a number.
Eg:- abs(-12)
It returns the value 12
abs(+12)
It returns the value 12
sqr( )
          It produces the square root of the argument.
Eg:- sqr(64)
It returns the value 8
round( )
          It returns the rounded figure for a number with a specified number of decimal points.
Eg:- round(432.23454, 3)
It returns 432.234
int( )
          It displays the integer part of the given number.
Eg:- int(987.432)
It returns 987
String functions
len( )
It produces the number of characters ie, the length of the string.
Eg:- len(“Together to win”)
It produces 15 as the output.
lcase( )
          It is used to convert all the uppercase (Capital letters) into lowercase (small letters).
Eg:- lcase (“TOGETHER”)
Its output is ‘together’.
ucase( )
It is used to convert all the lowercase (small letters) into uppercase (Capital letters).
Eg:- ucase (“together”)
Its output is ‘TOGETHER’.
ltrim( )
          It eliminates the blank spaces of left side of the string.
Eg:- ltrim(“                       win here and here after”)
Its output is ‘win here and here after’
rtrim( )
          It eliminates the blank spaces of right side of the string.
Eg:- rtrim(“win here and here after                      ”)
Its output is ‘win here and here after’
trim( )
          It eliminates the blank spaces of left and right side of the string.
Eg:- trim(“                       win here and here after                      ”)
Its output is ‘win here and here after’
left( )
          It helps us to bring a specified number of characters from the left side of the string.
Eg:- left(“Together to win”, 8)
It produces the output as ‘Together’
right( )
          It helps us to bring a specified number of characters from the right side of the string.
Eg:- right(“Together to win”, 3)
It produces the output as ‘win’
mid( )
          It helps us to bring a specified number of characters from the middle of the string.
Eg:- mid(“Together to win”, 10, 6)
It produces the output as ‘to win’
instr( )
          It is used to produce the position number of a second string on the first string.
Eg:- instr(“Together to win”, “to”)
It produces the position number as 10
Date functions
date( )
          It returns the current system date.
Eg:- msgbox date ( )
It display 30/08/2008 on message box.
now( )
It display the current system date and time.
Eg:- msgbox now( )
It display 30/08/2008 12:25:10 PM on message box.
time( )
It display the current system time.
Eg:- msgbox time( )
It display 12:25:10 PM on message box.
weekday( )
          It returns the integer number corresponding to the day ranges from 1 to 7.
Eg:- msgbox weekday( )
It displays 2 if the day is Monday.
month( )
          It returns the integer number corresponding to the month ranges from 1 to 12.
Eg:- msgbox month( )
It displays 2 if the month is February.
day( )
          It returns the integer number corresponding to the date ranges from 1 to 31.
Eg:- msgbox day( )
It displays 2 if the date is 2.
year( )
          It returns the integer number corresponding to the year.
Eg:- msgbox year( )
It displays 2oo8 if the year is 2008.
datediff( )
          It help us to find the number of days, months, minutes, seconds and year between two dates.
Eg:- MsgBox DateDiff("d", #8/10/2008#, #8/30/2008#)
It produces 20 on the text box. Here the letter‘d’ used to find the number of days.(m,n,s,y).
Conversion functions
val( )
          It is used to convert the string data to a numeric data in atext box.
Eg:- val( text1.text)
Here the text box value taken as a number.
str( )
It treat a number as a string.
Eg:- str(300)
Here 300 is treated as “300”
asc( )
          It converts the character to a corresponding ASCII code.
Eg:- asc(“A”) returns the value as 65 and asc(“a”) returns the value as 97.
chr( )
It converts the ASCII code to corresponding character.
Eg:- chr(65) returns the character as “A” and chr(97) returns the character as “a”.
format( )
          It uses to display a numeric data in a specified format.
Eg:- format(75657.938849, “#,##.##”)
Its output is 7,5657.93
Miscellaneous functions
line( )
          To draw lines on form object by specifying starting coordinate and ending coordinate with color
Eg:- line(500,1000)-(500,2000), Vbblue
circle( )
          It is used to draw circle on a form object with center point, radius and color.
Eg:- circle(3000,2000), 1000, vbred
rgb( )
          It is used to specify color combination of red, green and blue. Here maximum value of each color is 255 and minimum is 0
Eg:- rgb(255,0,0)
It produces red color.
rnd( )
          It returns a random number less than 1 and greater than or equal to zero.
Eg:- msgbox rnd( )

No comments:

Post a Comment

 

Welcome Plus one ,How you rate IT Club SKHSS

Members

Contact Form

Name

Email *

Message *

Visitors Counter

Most Reading

SREEKRISHNA HIGHER SECONDARY SCHOOL- IT CLUB. Powered by Blogger.