Visual Basic 'Net AAPP00832 VBN - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Visual Basic 'Net AAPP00832 VBN

Description:

If you have mastered this topic, you should be able to use the ... label1.caption = FtoC(Fahrenheit) End Sub. Private Function FtoC(fahr As integer) As double ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 22
Provided by: sunitabal
Category:
Tags: vbn | aapp00832 | basic | caption | net | visual

less

Transcript and Presenter's Notes

Title: Visual Basic 'Net AAPP00832 VBN


1
Visual Basic .NetAAPP00-8-3-2 VBN
Functions Lecture 9
2
Topic Structure of the lesson
  • function procedures
  • exit sub and exit function
  • generating (pseudo) random numbers

3
Learning Outcomes
  • At the end of this lecture you should be able to
  • Write a Function Procedure
  • Use Exit Sub and Exit Function statements
  • Generate random numbers

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 exams
  • Function
  • Exit
  • RND

5
Functions
  • A function is similar to a procedure in that it
    aids in modular design, i.e. breaking a problem
    down into smaller problems.
  • Benefits of modular design
  • isolate problems
  • easier to develop and maintain code
  • aids software re-useability

6
Different Modular Techniques
  • Visual Basic has the following modular structures
  • event procedures
  • sub procedures
  • functions
  • The major difference between sub procedures and
    functions is that a function returns a value to
    the calling procedure.

7
Function Format
  • Private Function FunctionName(var1 as type1,var2
    as type2,.) as datatype
  • statement(s)
  • FunctionName expression
  • End Function
  • Called by
  • X FunctionName(arg1, arg2, )
  • Parameters and arguments MUST AGREE in type and
    number.

parameter list
argument list
8
Example
  • Option Explicit
  • Private Sub cmdConvert_Click()
  • Dim Fahrenheit as integer
  • Fahrenheit InputBox(Temperature?)
  • label1.caption FtoC(Fahrenheit)
  • End Sub
  • Private Function FtoC(fahr As integer) As double
  • FtoC (5 / 9) (fahr - 32)
  • End Function

9
Exercise 1
  • Write a function to accept the radius of a circle
    from a textbox and display the area in a message
    box.
  • Formula Area pi r r
  • pi 3.142

10
Solution
  • Option explicit
  • Const PI 3.14159
  • Private sub comand1_click()
  • Dim radius, area as double
  • radius CDbl(InputBox (Enter Radius))
  • area areaofcircle(radius)
  • MessageBox.Show (The area of a circle with
    radius
  • radius is area)
  • End sub
  • Private function areaofcircle(rad as double) as
    double
  • areaofcircle PI rad rad
  • End Function

11
Exercise 2
Write a 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
12
Exit Function
  • 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

13
Exit Function Example
  • Private sub cmddivide_click()
  • Dim numerator , denominator as integer
  • Dim result as string
  • Numerator CInt(txtnum.text)
  • Denominator CInt(txtden.text)
  • Result divide(numerator, denominator)
  • If result then
  • MessageBox.Show ( Divide by Zero attempted)
  • Else
  • MessageBox.Show (result)
  • End if
  • End sub

14
Exit Function Example
  • Private function divide(n as integer, d as
    integer) as string
  • Dim answer as integer
  • If d 0 then
  • exit function
  • Else
  • answer n / d
  • divide Integer division yields ans
  • End if
  • End Function

15
Random Number Generation
  • the built-in VB function, RND, returns a random
    number in the range 0 lt random number lt 1
  • To generate a random number between some other
    range Low High, use the following formula
  • rand Low Int((High Low 1) Rnd)
  • Example
  • Dieface 1 Int(6 rnd()) to generate random
    numbers from 1 to 6

16
Exercise 3
  • Write a program that will generate a random
    number from 5 to 100 when a command button is
    pressed.

17
Exercise 4
  • Write a Function procedure that takes two
    String arguments representing a given name and
    family name, concatenates the two Strings to form
    a new String with the family name first followed
    by a comma and the given name last and returns
    the concatenated String.

18
Exercise 5
  • Write a program that Inputs an Integer and
    passes it to function IsEven, which uses the
    Modulus operator to determine if the Integer is
    even.
  • The Function should take an Integer argument and
    return True if the Integer is even and False
    otherwise.

19
Summary of Main Teaching Points
  • Function Procedures
  • are often used to solve an equation
  • return a single value as the answer
  • the arguments in the function invocation must
    match the parameter list of the function in both
    type and number

20
Next Session
Arrays
21
Question and Answer Session
Q A
Write a Comment
User Comments (0)
About PowerShow.com