Procedure
A procedure is a program that carries out a set of actions. It may or may not expect input parameters. It may perform output. An application may consist of several sub procedures each of which performs a particular job.
Function
A function is a program that returns a single value to its caller. It may or may not expect input parameters.
Parameter
A parameter is data passed to a function or procedure when it is called.
NOTE:
It is not precise to say “a parameter is a value passed…”. An array can be passed as a parameter, which of course is not a single value.
Why pass parameters?
When you define a procedure or function, you can write it in such as way as it will expect one or more input parameters when it is called. This technique is the cornerstone of modular applications which include many procedures and functions that need to exchange data. Modular design allows software developers to begin writing the functions, procedures and code libraries that will make up the application. Sub-tasks that need to be performed more than once may be implemented as functions, facilitating code reuse, not to mention simplified development and maintenance. Being able to exchange pass data between the modules is essential in a modular application, and the is achieved by parameter passing.
Example
The example shown here is a procedure named AddTogether that expects to be passed two Integer values when it is called. It then adds these values together and outputs the result in a message box. Parameters are declared within parentheses (brackets) immediately after the procedure name and are separated by commas. Notice that the As clause is needed to specify the data type of each parameter.
Sub AddTogether(iNumber1 As Integer, iNumber2 As Integer)
MsgBox iNumber1 + iNumber2
End Sub
The procedure AddTogether would be called as follows:
Call AddTogether(3, 5)
You can also pass the values of variables to a procedure that expects parameters. For example, you can also call AddTogether follows:
Sub myCaller()
Dim X As Integer, Y As Integer
X = 6
Y = 8
Call AddTogether(X, Y)
End Sub
When a function or procedure that expects parameters is called, parameters must be passed in the order in which they were declared. For example, when myCaller calls AddTogether, then as far as AddTogether is concerned, iNumber1 is 6 and iNumber2 is 8. This makes no difference in the specific example because the two numbers are simply added together, but it could be important if the two numbers were used in another way.
Passing parameters by value or by reference
With most programming languages, the default behaviour is to pass parameters By Value. The definition of AddTogether could be more precisely written as:
Sub AddTogether(ByVal iNumber1 As Integer, ByVal iNumber2 As Integer)
This means that for each parameter the caller is actually passing a copy of the original data in X and Y.
Alternatively, AddTogether could have been defined as follows:
Sub AddTogether(ByRef iNumber1 As Integer, ByRef iNumber2 As Integer)
This time the parameters are being passed By Reference; data values are not passed, rather, pointers to memory locations of X and Y. This means that if AddTogether modifies the contents of these memory locations, the modified contents would be visible to the calling program once it regains control.
Passing parameters By Value is considered safer and is also more efficient, because data is available to the program that has been called via the call stack.