0

In Visual Basic , a subroutine can be called in two ways .For example ,

If abc is a subroutine which takes an integer as a value then it can be called in both of the following ways :

call abc(i) and

abc i.

If both achieve the same output then why do we have two different ways to call the subroutine ,I guess there must be some difference in the internal way they are executed or something in terms of compilation .

Are there any other such languages with feature like this , in a general case what is the difference between these two ways in all such languages ?

  • Backward compatibility. So that all the code which said `Call mySub(myParam)` didn't have to be rewritten. Similar to still being able to enter line numbers in VBA (e.g. `10 x = 0` `20 For I = 1 To 5` `30 x = x + I` `40 Next I`) – YowE3K May 21 '17 at 09:31
  • The brackets are needed when you are expecting a return value from a process (a function), and must be omitted when you are not expecting one. Except when you have only one parameter, because then you can chose to have or not have brackets regardless of the existence of a return value. Just to make things simple... – vacip May 21 '17 at 10:03
  • @vacip : But we don't expect any return value from subroutines , right ? – private ryan May 21 '17 at 10:24
  • Well, sure we do, from Functions. But you are right, not from Subs. – vacip May 21 '17 at 10:53

1 Answers1

1

There is no difference in how VBA handles it, it's just there for backwards compatibility with previous VBA versions.

The only difference now lies in the notation: Using call you must use parentheses around the parameters, without call there second must not be parentheses.

Also see Should I use Call keyword in VB/VBA? for edge cases where the call statement could actually be useful.

Leviathan
  • 2,468
  • 1
  • 18
  • 24