# Call
{: .no_toc }

Transfers control to a **Sub** [procedure](/tB/Gloss.md#procedure), **Function** procedure, or dynamic-link library (DLL) procedure.

Syntax:

- **Call** *name* **(** [ *argumentlist* ] **)**  
  When the **Call** keyword is specified, the *argumentlist* must be enclosed in parentheses.  
  
- *name* **(** [ *argumentlist* ] **)**  
  Without the **Call** keyword, the *argumentlist* can be optionally enclosed in parentheses,  
  
- *name* [ *argumentlist* ]  

*name*
: The name of the procedure to call

*argumentlist*
: *optional* A comma-delimited list of variables, arrays or expressions to pass to the procedure. Components of *argumentlist* may include the keywords **ByVal** or **ByRef** to describe how the arguments are to be passed to the called procedure.

The **Call** keyword is not required when calling a procedure. However, when the **Call** keyword is used to call a procedure that requires arguments, *argumentlist* must be enclosed in parentheses. When the **Call** keyword is omitted, the parentheses around *argumentlist* must also be omitted. When either **Call** syntax is used to call any intrinsic or user-defined function, the function's return value is discarded.

To pass a whole array to a procedure, use the array name followed by empty parentheses.

Enclosing a single argument in its own parentheses, as in `MyProc (x)`, makes it an expression: the procedure receives a copy, even for a **ByRef** parameter. See [Passing arguments ByRef and ByVal](/tB/Core/Sub.md#passing-arguments-byref-and-byval).

A call can pass an argument with `ByVal` for a `ByRef` parameter of a twinBASIC procedure. The procedure then receives the value as the parameter's address, so `ByVal 0&` passes a NULL pointer:

```tb check_build projname=call-byval-null
Public Function ArgumentAddress(ByRef h As Long) As LongPtr
    ArgumentAddress = VarPtr(h)
End Function
```

```tb check_build check_run projname=call-byval-null
Dim n As Long
Debug.Print ArgumentAddress(n) <> 0     ' True
Debug.Print ArgumentAddress(ByVal 0&)   ' 0
```

> [!NOTE]
> VB6 refuses `ByVal` on an argument for a `ByRef` parameter of a procedure written in Basic, with the error *Type mismatch*.

### Example

This example illustrates how the **Call** statement is used to transfer control to a **Sub** procedure, an intrinsic function, and a dynamic-link library (DLL) procedure.

```tb check_build
' The Declare statement must be Private in a Class Module, but not in a standard Module.
Private Declare Sub MessageBeep Lib "user32" (ByVal wType As Long)

Sub PrintToDebugWindow(AnyString)
    Debug.Print AnyString    ' Print to the Debug Console.
End Sub

Sub CallExamples()
    ' Call a Sub procedure: control passes to PrintToDebugWindow above.
    Call PrintToDebugWindow("Hello World")

    ' Call an intrinsic function. Its return value, the program's
    ' task ID, is discarded.
    Call Shell("notepad.exe", vbNormalFocus)

    ' Call a Windows DLL procedure.
    Call MessageBeep(0)
    MessageBeep 0    ' Call again without the Call keyword.
End Sub
```

### Named arguments
{: index="named parameters" }

An argument can be passed by the name of its parameter instead of by its position, as *name* **:=** *value*. Named arguments can come in any order, and an **Optional** argument that isn't named is left out, so there is no need to count commas to skip one. The name is the parameter's name in the procedure's declaration; for a built-in procedure, it is the name its reference page gives, such as *prompt* and *title* for [**MsgBox**](/tB/Modules/Interaction/MsgBox.md).

A call can start with positional arguments and continue with named ones, but once an argument is named, every argument after it must be named too. A procedure with a [**ParamArray**](/tB/Core/ParamArray.md) parameter can't be called with named arguments.

```tb check_build
Sub Greet(Name As String, Optional Greeting As String = "Hello", Optional Suffix As String = "!")
    Debug.Print Greeting & ", " & Name & Suffix
End Sub

Sub TryNamedArguments()
    Greet "Ada", Suffix:="?"                    ' Hello, Ada?  -- Greeting left out
    Greet Suffix:=".", Name:="Ada"              ' Hello, Ada.  -- in any order
    Call Greet("Ada", Greeting:="Hi")           ' Hi, Ada!
    MsgBox Prompt:="Done", Title:="Named arguments"
End Sub
```

### See Also

- [**Declare** statement](/tB/Core/Declare.md)
- [**Function** statement](/tB/Core/Function.md)
- [**Sub** statement](/tB/Core/Sub.md)
- [Passing arguments ByRef and ByVal](/tB/Core/Sub.md#passing-arguments-byref-and-byval)
- [Optional arguments and default values](/tB/Core/Sub.md#optional-arguments-and-default-values)
- [named argument](/tB/Gloss.md#named-argument) in the Glossary

