Subject: VB code that calls a function
Posted By: mikefemi Post Date: 1/11/2006 4:23:01 AM
Plesae, can somebody help me write a VB6 Code to call a Function or Procedure.

Thanks

Reply By: Bob Bedell Reply Date: 1/11/2006 5:12:04 AM
Here's kinda' the basics. Notice that functions return a value, where as sub procedures don't. If you pass a variable to a procedure by reference, however, you can modify the variable's value in the calling procedure. This has the same effect as assigning the return value of a function to the variable.

Run the code by typing either CallProcedure or CallFunction in the Immediate Window and hitting enter. Set a break point on the procedure or function header and step through the code to see how they work. Watch the variable values change in the Locals Window.

Sub CallProcedure()
    Dim intX As Integer, intY As Integer, intSum As Integer
    
    intX = 2
    intY = 2
    
    'intSum us passed by reference
    Call sCalculate(intX, intY, intSum)
    
    Debug.Print intSum
    
End Sub

Sub CallFunction()
    Dim intX As Integer, intY As Integer, intSum As Integer
    
    intX = 2
    intY = 2
    
    ' Assign functions return value to a variable
    intSum = fCalculate(intX, intY)
    
    Debug.Print intSum
    
End Sub

Sub sCalculate(ByVal intA As Integer, _
                    ByVal intB As Integer, _
                    ByRef intAddress As Integer)
      
    intAddress = intA + intB
        
End Sub

Function fCalculate(ByVal intA As Integer, _
                          ByVal intB As Integer) As Integer
      
    fCalculate = intA + intB
    
End Function

HTH,

Bob


Go to topic 38578

Return to index page 398
Return to index page 397
Return to index page 396
Return to index page 395
Return to index page 394
Return to index page 393
Return to index page 392
Return to index page 391
Return to index page 390
Return to index page 389