Sei sulla pagina 1di 21

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRAPRADESH
Name : K. Sreeramacharyulu
Designation : Lecturer
Branch : Commercial and Computer Practice
Institute : Govt. Polytechnic, Srikakulam
Year/Semester : V Semester
Subject : VISUAL BASIC - I
Sub. Code : CCP-503
Topic : Programming Fundamentals
Duration : 50 Minutes
Sub. Topic : Functions concepts in VB
Teaching Aids : PPT, Clips and Images
CCP503.37 1
Objectives

On completion of this period you will be


able to Know…

 The Function Sub program concepts in VB


 Types of Functions in VB
 The User defined Functions in VB

CCP503.37 2
RECAP
 The concepts of Sub-Programming in VB

 The uses of Sub-Programming.

 The types of Sub-Programs such as Procedure sub


program,Function sub program etc,.

CCP503.37 3
FUNCTIONS IN VB
 Is a block of code similar to a subroutine, but returns a
value / result to calling code
 There are some Built-in functions like Sin() & Cos(),
RGB() & QBCOLOR() we already learnt
 Syntax:
 Private / Public Function name (arguments) As Type
 Sample Example for a function:
Function NextDay() As Date
NextDay = Date() + 1
End Function
CCP503.37 4
TYPES OF FUNCTIONS

Basically there are two types of functions in


VB
– User defined Functions: Further User
defined Functions are two kinds
a. Non-Recursive Functions
b. Recursive Functions

4. Library Functions ( In-Built / Built-in)

CCP503.37 5
User defined Non-Recursive Functions

While dealing with User defined functions, the


following points are to be remembered.
1) Function Definition (called Program)
2) Function calling (Reference) at calling
program.
Where as in Library functions, the Definition part is
not there because they are predefined in the system
Itself ( i.e in VB Package)

CCP503.37 6
Definition part includes
 Name of the function,
 return data type,
 parameter to be passed if necessary,
 return statement etc,.

CCP503.37 7
General Syntax of the User defined Function

 Block of statements is placed within a pair of


Function / End function statements and can be
invoked by its name
General Syntax:
[Private/Public] [Static] Function <Procedure name>
( [Parameter1 as Datatype, Parameter2 as
Datatype, …….]) as Datatype
<statements1, Stat2, …..>
End Function

CCP503.37 8
 A function returns a single value through its
name

 A function accepts values through arguments


within brackets following function name

CCP503.37 9
Sample Example on User defined function

Public Function Multiply( a as Integer, b as Integer) As Integer


Multiply=a*b
End Function
Rem Calling Program can be an Event or Other Function, or
Rem another Procedure as follows
Private Sub command_ click
MsgBox “ Multiplication of two Numbers is “ & Multiply(16,10)
End Sub

CCP503.37 10
Steps to create a sample Function
1. Open a new project
2. Design the program as per requirements
3. Go to code window, click on Tools menu, choose add
Procedure, then VB will display the following dialogue
box
4. Choose the Function option and then type Name of the
Function, click OK button
5. Then write the code whatever you want .
6. Use the function with function name

CCP503.37 11
CCP503.37 12
Program 1 on Non-Recursive User defined
Functions
Program to calculate future value of a certain amount ,
based on interest rate &number of years

 The calculation based on compound interest rate.

 Open a new Standard EXE project & name it as Form1

 Add two buttons ”CalculateFutureValue” & ”exit” ,Text &


Label boxes & enter code for buttons & “FV” Function as
in Fig 37.1

 Now run application & click “CalculateFutureValue”


observer changes
CCP503.37 13
Fig 37.1 CCP503.37 14
Program 2 on on Non-Recursive User defined
Functions

Program to compute examination grades based on the


marks that a student obtained
 Open a new Standard EXE project & name it as
Form1
 Add two buttons ”Compute” & ”exit” , one Text
“mark” & three Label boxes, name one label box as
grading other labels appropriately
 Enter code for buttons & “grade” Function as in Fig
37.2
 Now run application & click “compute” observer
changes
CCP503.37 15
CCP503.37 16
Fig 37.2
SUMMARY

In this Session, you learnt about…


 The concepts of Functions in VB
 Types of Functions in VB
 Writing programs using functions concepts

CCP503.37 17
FREQUENTLY ASKED QUESTIONS

1. Define Function and mention the advantages of it.

3. List different types of Functions supported in VB?

5. Write a program in VB to accept a number and then


compute its Factorial and then print in a text box on
the form provided.

CCP503.37 18
QUIZ

2. Function reference is specified at

A. Calling Function / Procedure


B. Called Functions
C. Called Event
D. Called Procedure

2. Function returns the value through --------------


A. Through its name
B. Through its function header
C. Through return statement
D. Through its definition CCP503.37 19
1 Recursive functions are ready made.

A. True
B. False

4 The no. of values that can be returned by a function

C. 1
D. 2
E. 3
F. 4

CCP503.37 20
ASSIGNMENT

1. Write a function that accepts a date of birth and returns


age in years

CCP503.37 21

Potrebbero piacerti anche