0% found this document useful (0 votes)
72 views21 pages

Lec 09 Functions

This document outlines a lecture on functions in Visual Basic .NET. It discusses defining function procedures, using exit statements, and generating random numbers. The key topics covered are writing and calling functions, returning values, and using exit statements to exit functions early. Examples are provided to demonstrate defining functions that calculate math equations and concatenate strings. Exercises instruct students to write functions to calculate circle area and volume, generate random numbers, and determine if a number is even.

Uploaded by

Yovin Lekamge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views21 pages

Lec 09 Functions

This document outlines a lecture on functions in Visual Basic .NET. It discusses defining function procedures, using exit statements, and generating random numbers. The key topics covered are writing and calling functions, returning values, and using exit statements to exit functions early. Examples are provided to demonstrate defining functions that calculate math equations and concatenate strings. Exercises instruct students to write functions to calculate circle area and volume, generate random numbers, and determine if a number is even.

Uploaded by

Yovin Lekamge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Visual Basic .

Net
AAPP00-8-3-2 VBN

Functions
Lecture 9
Topic & Structure of the lesson

•function procedures
•exit sub and exit function
•generating (pseudo) random numbers

AAPP00-8-3-2 VBN Visual Basic .Net Slide 2 (of 30)


Learning Outcomes

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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 3 (of 30)


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:

1. Function
2. Exit
3. RND

AAPP00-8-3-2 VBN Visual Basic .Net Slide 4 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 5 (of 30)


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.

AAPP00-8-3-2 VBN Visual Basic .Net


Function Format
Private Function FunctionName(var1 as
type1,var2 as type2,….) as datatype
statement(s) parameter list

FunctionName = expression
End Function
Called by: argument list

X = FunctionName(arg1, arg2, …)
Parameters and arguments MUST AGREE in
type and number.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 6 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 7 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 8 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 9 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 10 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 19 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 20 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 21 (of 30)


Random Number Generation

• the built-in VB function, RND, returns a random


number in the range:0 <= random number < 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 22 (of 30)


Exercise 3

• Write a program that will generate a


random number from 5 to 100 when a
command button is pressed.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 23 (of 30)


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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 26 (of 30)


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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 27 (of 30)


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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 28 (of 30)


Next Session

Arrays

AAPP00-8-3-2 VBN Visual Basic .Net Slide 30 (of 30)


Question and Answer Session

Q&A

AAPP00-8-3-2 VBN Visual Basic .Net Slide 29 (of 30)

You might also like