Tambien hay una version en castellano de este post, click aca.
Welcome to this post, today we’ll talk about two inherited methods from Visual Basic.
These are the functions and procedures, also known as subroutines, that can be used to create new statements. Let’s watch the syntax to define a procedure:
sub proc_name(arguments)
... statements ...
end sub
Always starts with sub, then we assign an identifier name for use it in our code. The arguments are optionals but we can use it to pass information at our block. In the block, we have the statements of our procedure and we close it with end sub. Let’s watch the syntax of a function:
function func_name(arguments)
... statements ...
func_name = return_value
end function
These start with the word function, then we assign an identifier name for use it in our code. The arguments are optional too and have the same behaviour. In the block, we have the statements but function must return a value unlike with procedure. This value is returned assigning the final value to the name of the function. The block is closed with end function. Let’s make an example to understand the differences and how it works, make a new file and put this code:
dim value_1
sub sum_proc(value_1, value_2)
total = value_1 + value_2
msgbox total,,"sub sample"
end sub
function sum_func(value_1, value_2)
total = value_1 + value_2
msgbox total,,"function sample"
sum_func = total
end function
sum_proc 10, 20
value_1 = sum_func(10, 20)
msgbox value_1/2,,"function final value"
First, we declare a variable that we’ll use later. The next is define a procedure with the name of sum_proc. This receives two arguments, in the block we sum these values and store it in a new variable. Finally, we show this value with a msgbox.
Now, we define a function with the name of sum_func, this receives two arguments too, make the sum of these and show the final value with msgbox as sum_proc but in this case, we return the value stored in total.
With the procedure and function defined, we call to the precedure with these values, in this case doesn’t need parenthesis, let’s see its output:

It returns the sum of the values. It doesn’t use parenthesis because we don’t assign it to a variable. It applies to a function too. Then, we use the function with two values and store it in the variable declared it at the beginning. Remember that a function returns a value and in this case we use parenthesis because we assign the value. With this call, we show this message:

We’ve the same message because we do the same operation. Finally, we use msgbox to show the value returned by the function divided by two. Let’see its output:

The differences between the procedure and the function is the return of the value, all else is the same. It’s not mandatory store the returned value but if we don’t need it is preferable use a procedure.
In summary, we saw procedure and functions, how they’re, how they work, similarities and differences, and an example to watch them in action. I hope you’ve found it useful. You can follow me on this social networks:


Donatión
It’s for site maintenance, thanks!
$1.50
