passing a value to a function

Milan

Registered User.
Local time
Today, 20:33
Joined
Feb 17, 2002
Messages
85
how do i pass a value from a sub procedure in a form, to a function in a module and get the result back into the procedureQ

Thanks
 
The items between the "(" and ")" of a sub or function are the parameters that you pass.

Example:

Code:
'This function takes the value of sigNum and Multiplies it by .5
'To call this function would be something like this

'Dim x as Single
'x=MyFunction(1)

'x will = .5

Function MyFunction(ByVal sigNum as Single) as Single

  MyFunction=sigNum * .5

End Function
 
By passing parameters.


Consider this Subroutine on a form:

Code:
Private Sub Whatever()

    Dim intValue as Integer

    intValue = InputBox("Please enter a number", "Example")

    intValue = SquareValue(intValue)

    MsgBox intValue

End Sub


And then this code in a module:

Code:
Public Function SquareValue(ByVal intNumber As Integer) As Integer

    SquareValue = intValue * intValue

End Function


Let's look at what is happening.

1) We have created an integer in which we will store a value;

2) Using the input box in this case we are asking for a numeric value;

3) Taking the numeric value offered by the user we want to calculate, in this example, the square of the number;

4) So we say that the value is equal to the function name and pass the argument (the value itself) to the function;

5) The function in the module receives the value

6) The function performs the calculation and assigns it to the function name;

7) The code returns to our sub and makes the value equal to the result of the function;

8) And just to prove it works, we message box the result.


You can send as many values to the function as long as it is written to receive that number of values.
 

Users who are viewing this thread

Back
Top Bottom