Sie sind auf Seite 1von 22

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 : Procedures and Functions in VB
Teaching Aids : PPT, Clips and Images

CCP503.36 1
Objectives

On completion of this period, you would be


able to Know…

 Sub programs concept in VB

 Procedures and Functions concepts in VB

CCP503.36 2
RECAP

 Earlier you have learnt only event driven programming

 Event procedures are blocks of code that are executed


when a specific event occurs, such as the Single Click
or Double Clicking of a command button

CCP503.36 3
VB AS AN EVENT-DRIVEN LANGUAGE
 Nothing happens in a VB Project until some event occur
 Once an Event is detected, the project finds series of
instructions related to event, called an event procedure
 That procedure is executed and then program control
returns to the form
 Event Procedures are the actual computer programming
instructions that are saved with the form, with extension
‘.frm’
 Due to above reasons, VB is called as Event-Driven
Procedural Language

CCP503.36 4
INTRODUCTION TO SUB-PROGRAMS
 In any Programming Language such as C / Pascal /
Fortran / C++ / Java etc,. The code used in VB or any
other High Level programming Language will not be a
monolithic listing
 No programmer can write a single and big in size of
Project
 A big project is usually divided into some set of smaller
units, which are self-contained segments
 These smaller units are called Modules.
 Sub programs can be repeatedly used from same form
or other forms or even other applications
CCP503.36 5
ADVANTAGES OF SUB PROGRAMS
When you are incorporating the Sub-
Programs in to the VB Program, the
following advantages exists.

 Easy to Understand
 Easy to Debug
 Reusability of code

CCP503.36 6
SUB-PROGRAMS IN VB

 Visual Basic Programming will support the


following types of sub-programs

1. Sub Procedures / Subroutines (Procedure


Sub-Program) which do not return a value

3. Function Procedures (Function Sub


Program), which return a value

CCP503.36 7
SUB PROCEDURES

Definition:
 Block of statements that carries out a well-defined task
 Block of statements is placed within a pair of sub / End
sub statements and can be invoked by its name
General Syntax:
[Private/Public] [Static] sub <Procedure name> ( [Arguments])
<statements>
End sub
 Sub & end sub are called wrapper lines which can be
added with Tools ->Add procedure

CCP503.36 8
Example:
 The following subroutine displays the current date
in a Message box and can be called by its name ,
Showdate:

Sub ShowDate()
MsgBox Date()
End sub

 The statements in a subroutine are executed ,


and when End Sub statement is reached , control
returns to the calling program

 It is possible to exit a subroutine using Exit


statement

CCP503.36 9
CLASSIFICATION OF PROCEDURES

A Sub Procedure can be divided into two types:

1. General Procedures

3. Event Procedures

CCP503.36 10
1. General Procedures

 Special purpose blocks of code that perform a


single task
 We can create general procedures in all types of
modules
 Perform tasks common to all the applications such
as connecting to a database, retrieving data,
making complicated calculations
 Specific tasks related to a single application such
as generating the PIN No. of a student

CCP503.36 11
Syntax:

[Private/Public] [Static] sub <Procedure name> ( [Arguments])


<statements>
End sub
Which is similar as in slide No.10

CCP503.36 12
2. Event Procedure
 Blocks of code that are executed when a specific event
occurs , such as the Click of a command button or
Loading/clicking of a form etc.,

Syntax:
Private Sub <ObjectName_EventName> ([Arguments])
<statements>
End sub

If necessary an early exit from procedure done by Exit


sub
CCP503.36 13
PROGRAM ON PROCEDURE SUB-PROGRAM
Follow the steps to develop application: From Fig 34.1

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

 Add two buttons ”BranchName” & ”exit” enter code for


BranchName button as well as “BranchinPin” procedure

 Now run application & click “BranchName”

 Enter Pin Number & click OK , to check whether that PIN


related to CCP/ DME / DEEE / DCE branch

CCP503.36 14
CCP503.36 15
CCP503.36 16
Fig 36.1
SUMMARY
 About the concepts of Sub-Programming in Computer
High Level Languages.
 Types of Sub-Programs, the real advantages of Sub-
Programming.
 The concepts of Procedures and functions

 Writing Programs in VB by incorporation of


Procedures concepts.

CCP503.36 17
FREQUENTLY ASKED QUESTIONS
1. Explain Sub-Programming in any Computer High Level
Languages?
2. List and explain in brief about the types and advantages
of Sub-Programs?
3. What is a Procedure in VB and give a suitable
example?

CCP503.36 18
QUIZ
1 A subprogram that won’t return anything to
the calling program is…

B. Procedure
C. Function
D. Event Procedure
E. Module Program

CCP503.36 19
2. A block of code, that returns a value / result to
calling program is called…

A. Procedure
B. Function
C. Subprogram
D. Main Program

CCP503.36 20
1 The advantage of the Sub-Programming in any
Programming Language is ?

A. Easy to Understand

B. Easy to debug the code

C. Re-usability of code if necessary

D. All of the above

CCP503.36 21
ASSIGNMENT
1. Write a procedure that shows the day name of
the system date

CCP503.36 22

Das könnte Ihnen auch gefallen