> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Every Function in VBA Returns a Value
- URL: https://nolongerset.com/every-function-has-a-return-value/
- Published: 2023-03-11T04:40:27.000Z
- Updated: 2026-05-08T12:51:09.000Z
- Description: Whether you assign one explicitly or not, every function in VBA has a return value.
- Author: Mike Wolfe
- Tags: Subs vs. Functions, #Import 2026-05-20 02:59

This article is one in a [series on Subs vs. Functions](https://nolongerset.com/subs-vs-functions/) in VBA.

---

One of the "additional facts" from my Subs vs. Functions article was this:

> A `Function` returns a value even if you don't return one explicitly

This is closely related to the fact that [every Function has a return *type*](https://nolongerset.com/every-function-has-a-return-type/), whether you set one or not.

## Explicitly Returning a Value from a Function

Programmers from other languages who use VBA often have trouble figuring out how to return a value from a function.

Why? Because almost every other programming language on the planet uses the `Return` keyword to return the value of a function. 

This includes 9 of the top 10 languages currently listed in the [TIOBE index](https://www.tiobe.com/tiobe-index/), including VB.NET!

1. Python: `return x`
2. C: `return x;`
3. Java: `return x;`
4. C++: `return x;`
5. C#: `return x;`
6. VB.NET: `Return x`
7. JavaScript: `return x;`
8. SQL: `SELECT x;`
9. PHP: `return $x;`
10. Go: `return x`

The lone exception in the above list is SQL, and TIOBE is honestly stretching the [definition of a programming language](https://www.tiobe.com/tiobe-index/programminglanguages%5Fdefinition/) by including it in the list.

*Welp, apparently [SQL is Turing Complete](https://stackoverflow.com/a/7580013/154439)–though only if you include its support for common table expressions; who knew?*

### The `Return` Keyword in VBA

VBA does have a `Return` keyword. The problem is that [it's used](https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/gosubreturn-statement) to return to the call site of a `GoSub` call. 

*What's *GoSub*? you ask. It doesn't matter. It's evil. There is no need for it. Forget I brought it up.*

Because the `Return` keyword is being used elsewhere, we still needed some way to return the value of a function in VBA.

### Setting the Value to the Name of the Function

The way to return a value from a function in VBA is to assign a value to a variable that shares the name of the function. Here's how the VBA Language Specification defines it in [Section 5.3.1 Procedure Declarations](https://learn.microsoft.com/en-us/openspecs/microsoft%5Fgeneral%5Fpurpose%5Fprogramming%5Flanguages/ms-vbal/227005ad-78fb-479f-8145-fa3b8b610386):

> A function declaration implicitly defines a local variable, known as the function result variable, whose name and declared type are shared with the function and whose scope is the body of the function.

Here's an example:

```vba
Function Foo() As Integer
    Foo = 42
End Function
```

The function `Foo` returns the literal value 42.

### Exiting a Function in VBA

In most other programming languages, the **return** keyword does double duty; it sets the return value for the function AND it exits immediately.

In VBA, these two duties remain separate. If you want to set a Function's return value and exit immediately in VBA, you need to use two different lines of code:

```vba
Function WhatIsToday() As String
    If Month(Date) = 3 And Day(Date) = 15 Then
        WhatIsToday = "The Ides of March.  Caesar, look out!"
        Exit Function
    End If
    
    WhatIsToday = "Today is " & Format(Date, "mmmm d")
End Function
```

If we remove the `Exit Function` line in the above `WhatIsToday()` function, then on March 15 the function would return "Today is March 15" rather than the all-important warning to Julius Caesar. 

In fact, that exact programming error is what led to Caesar's untimely death. Really, it should surprise no one to know that VBA was in fact around in 44 B.C. and that it has not changed one bit in all that time.

## Implicitly Returning a Value from a Function

I said at the beginning that every function in VBA returns a value.

But what if you never assign a value to the implicit variable that shares the name of the function? In that case, what gets returned is the default value of the Function's type.

```vba
Function Baz() As Integer
End Function
```

The function `Baz()` above returns 0 because that is the default value of the Integer data type.

The default values for each VBA data type are defined in [Section 2.3, Variables](https://learn.microsoft.com/en-us/openspecs/microsoft%5Fgeneral%5Fpurpose%5Fprogramming%5Flanguages/ms-vbal/6fd5c596-7ce4-4b61-a8ce-42534465428f), of the VBA Language Specification:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2023/03/image-26.png)

### Implicitly Determining the Type of a Function

Of course, in order to know the default value of a function, you need to know its return type.

It's easy to tell what the return type of a function is if you've explicitly declared it using the `As` keyword, like with the Baz() function above. However, if you don't use the `As` keyword to define your function's type, it gets trickier. ***Most*** of the time, if you've left off the `As` keyword, your function will be implicitly typed as a `Variant` data type. That's not a guarantee, though.

It turns out there are four ways to set a Function's return type in VBA:

- **GOOD**: Explicitly
- **LAZY-BAD**: Implicitly
- **CLEVER-BAD**: Via Type-Suffix
- **JUST PLAIN EVIL**: Implicitly via `Def` Types

See my article, [Every Function Has a Return Type](https://nolongerset.com/every-function-has-a-return-type/), for more details:

[Every Function Has a Return TypeEvery Function in VBA has a return type even if you don’t explicitly define one. In fact, there are \*FOUR\* different ways to declare function return types.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2023/02/Designer--1-.jpeg)](https://nolongerset.com/every-function-has-a-return-type/)

The moral of the story is, 

> **Always explicitly declare the return type of a function.**

## The Default-est Value is Empty

If you create a new Access database,   
and declare a function with no explicit return type,  
and never assign a value to be returned,  
that function will be implicitly typed as a `Variant`,  
and its return value will be `Empty`.

```vba
Function ContentsOfMyMind()
End Function
```

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2023/03/image-27.png)

I knew it...

*Image by [Lisa Van Dorp](https://pixabay.com/users/lisavandorp-15954172/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=5018589) from [Pixabay](https://pixabay.com//?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=5018589)*