Question Arguments on Subroutine or Function

vash011390

Registered User.
Local time
Today, 10:43
Joined
Jul 29, 2015
Messages
29
I know it may be a stupid question but can someone enlighten me the use of parenthesis when creating Subroutine or Function in VBA?

Sub ()
<Code>
End Sub

Function ()
<Code>
End Function

I appreciate the explanation and reference sites where can I further read :D

Cheers,
vash
 
I presume it's just the syntax the compiler expects to find, so if it is missing (or incorrect) it reports an error, even if there are no arguments.
 
I see. Did you mean "what goes inside the brackets"?

If so, then the contents of the brackets are "arguments" or "parameters" that the main application is aware of, and that are required for use within the procedure. By passing the arguments to the sub, the passed arguments have scope only within the sub. A different procedure cannot "see" the variables referred to in another procedure's header. The names of the passed arguments do not have to be the same as the "real names" in the main application.

A developer can therefore create a self contained procedure to carry out a piece of work with all the external data contained in the procedure header - which can make development in a team much easier.

It is often a good idea to use a series of smaller subs and functions to manage a process, rather than use a single monolithic code block. It becomes both easier to follow, and the individual procedures can be more easily re-used.

You ought to research the subtle differences between byref and byval arguments
 
I realized that my question is quite vague... haha... anyway, many thanks for the explanation. Gemma is right that I want to know what is the importance of codes inside the parenthesis.. Will do my research now for byref and byvals..

Cheers,
vash
 
What matters more is the use of the parentheses when the function or sub is called and whether the Call keyword is used.

Normally a function requires parentheses around the argument list. A Sub must not have parentheses around the list.

Parentheses around a single parameter in a Sub cause it to be evaluated.

This curiosity is the one that catches many advancing developers when they first try to pass an object to a sub. If you put parentheses around a single parameter in a Sub then the argument will not be fed the object reference but the value of what is inside. So the textbox you try to pass ends up with the value and you get an "Object Required" error message.

However it changes if you use the Call keyword with a function. Call is used when the Return value of the function is not wanted. In effect it turns the function into a sub. Consequently parentheses around a single parameter will cause it to be evaluated.

If you are following my explanation you should be somewhat confused by now. ;)
I did post on this matter in great detail a few years ago on www.uteraccess.com and this site at a similar time. Unfortunately I can't find those threads right now. Well worth reading if you can find them.
 

Users who are viewing this thread

Back
Top Bottom