0% found this document useful (0 votes)
34 views

CCP503

This document discusses parameter passing techniques in Visual Basic programming. It explains that there are two types of parameter passing: by reference and by value. When passing by reference, any changes made to the parameter inside the procedure will affect the original variable. When passing by value, the procedure only receives a copy of the argument and changes do not affect the original. Examples are provided to demonstrate the difference between call by reference and call by value. The document also includes objectives, recaps, examples of code, and questions for students.

Uploaded by

api-3849444
Copyright
© Attribution Non-Commercial (BY-NC)
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)
34 views

CCP503

This document discusses parameter passing techniques in Visual Basic programming. It explains that there are two types of parameter passing: by reference and by value. When passing by reference, any changes made to the parameter inside the procedure will affect the original variable. When passing by value, the procedure only receives a copy of the argument and changes do not affect the original. Examples are provided to demonstrate the difference between call by reference and call by value. The document also includes objectives, recaps, examples of code, and questions for students.

Uploaded by

api-3849444
Copyright
© Attribution Non-Commercial (BY-NC)
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

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : K SREE RAMACHARYULU
Designation : LECTURER
Branch : Commercial & Comp. Practice
Institute : Govt. Polytechnic, SRIKAKULAM
Year / Semester :V
Subject : Visual Basic-I
Subject Code : CCP-503
Duration : 50 Minutes
Topic : Programming Fundamentals
Sub. Topic : Parameter Passing to Procedures in VB

Programming.
Teaching Aids : PPT, Animation
CCP503.40
Clips and Images 1
Objectives
On completion of this period, you would be
able to know

 The different types of Parameter Passing Techniques to


the Sub-Programs from the Calling Program in VB
Programming

CCP503.40 2
Recap

 Recursion concept

 Examples of Recursion

CCP503.40 3
Arguments (Parameters)
 Definition: An Argument is a value to be passed
to the Procedure / Function and on which the
procedure / Function usually acts.

 This is how Subroutines and Functions


communicate with the rest of application.

CCP503.40 4
Types of Arguments

 Basically Two types arguments are there in VB.


1. Formal Arguments: An arguments which are declared in
the Function / Procedure Header with in the parenthesis
 Ex: Function Fact( N as Integer) as Integer
N is formal argument
2. Actual parameters: These are the values passed to the
arguments declared in functions / procedures (i.e in the
function’s / Procedure’s reference)
 Ex : F=Fact (x) where x is actual argument

CCP503.40 5
Passing Arguments/ Parameters Technique

 In order to communicate to the Sub Programs by


the calling Event / Procedure, two different
techniques can be used.

– Parameter Passing By Reference: It is a


System default Technique
– Parameter Passing by Value. For which
“ByValue” Key word will be used.

CCP503.40 6
Passing Arguments by Reference

 It gives the procedure access to the actual Value of


the variable.
 The calling Procedure passes the address of the
variable in memory so that the Procedure can
change its value permanently.

CCP503.40 7
Example:

Function Add(A as Integer, B as Integer) As Integer


Add=A+B
A=0
B=0
End Function

 In the above Example Add Two Numbers and then


sets them to Zero.

 Next place a Command Button on the Form and


enter the following code in its Click Event

CCP503.40 8
Private Sub CmdAdd_ Click ()
Dim X as Integer,
Dim Y As Integer,
Dim Sum As Integer
X=10
Y=2
Sum=Add(X, Y)
Text1.text= X
Text2.text= Y
Text3.Text=sum
End Sub
CCP503.40 9
 This code will display the following results in the
Textboxes Text1, Text2, .. respectively

 Output: 0
0
12
The changes made to the function’s arguments take
effect even after the function has ended. The values
of the variables X and Y have changed permanently.

CCP503.40 10
Program at Design Time

CCP503.40 11
Program at Run time

CCP503.40 12
PROGRAM CODE

CCP503.40 13
Passing Arguments by Value

 When you pass an argument by Value, the


Procedure gets only a copy of the argument.

 Even if the Procedure changes it, the changes are


not permanent.

 The benefit of passing argument by Value is that


the argument values are isolated from the
Procedure and only the program in which they are
declared can change their values.
CCP503.40 14
Example: for Parameter Passing byValue

Function FH (ByValue Celsius As Single) As Single


Degree=(9/5) * Celsius + 32
End Function

 To see what the ByValue Keyword does, add a line that


changes the value of the argument in the function.
Function FH (ByValue Celsius As Single) As Single
FH=(9/5) * Celsius + 32
Celsius=0
End Function

CCP503.40 15
 Now call the function as follows.

Ctemp=InputBox(“ Enter Temperature in DegreeCelsius: “)


MsgBox ( Ctemp & “ Degrees Celsius “ & FH ((Ctemp)) & “ Degrees
is Fahrenheit”)

 If the value entered in the InputBox is 32, the following


message is to be displayed.

 “ 32 Degrees Celsius is 89.6 Degrees Fahrenheit”

CCP503.40 16
 Remove the Keyword ByValue from the function’s
definition and call the function later then,
 This time the program displays the following
message.
“ 0 Degrees Celsius are 89.6 Degrees Fahrenheit”

 When the Celsius argument was passed to the


Degrees() function, its value was 32. But the
function changed its value, and upon return it was 0.
Because the argument was passed by reference,
any changes made by the Procedure affected by the
variable permanently.

CCP503.40 17
SUMMARY

 The concept of Parameters


 The different types of Parameters like - the actual
parameters, the Formal Parameters etc.,
 The different types of Parameter Passing Techniques
to the Sub-Programs from the Calling Program in VB
Programming

CCP503.40 18
FREQUENTLY ASKED QUESTIONS

1. What is a parameter? Give an example?


2. List and explain different types of parameters?
3. What is passing parameter to a sub-Program?
Why it is needed?
4. What is call by Value?
5. What is the concept of call by reference?

CCP503.40 19
QUIZ
1. What is formal parameter?
A. Parameter in the Function’s header
B. Parameter in the Function’s reference
C. Parameter declared globally
D. Parameter declared locally

2. An actual parameter is at ---------------


A. The Header of a Procedure
B. The reference of a Function / Procedure
C. The Event in side
D. The event outside
CCP503.40 20
ASSIGNMENT
1. Write a program with procedure passing parameters
both by reference and by value

CCP503.40 21

You might also like