Local function/sub inside function/sub
Author Message Local function/sub inside function/sub

How do I define a local function (or sub) inside a function or sub?

I want to define a function locally as it is only used within the
outer function. My example code looks like this:

Function Combination(n As Integer, r As Integer) As Double

Function FactInterval(First As Integer, Last As Integer) As Double
Dim f As Double
Dim i As Integer

f = 1
For i = First To Last
f = f * i
Next i
FactInterval = f
End Function

Combination = FactInterval(n - r + 1, n) / FactInterval(1, r)
End Function

VB5 gives me 'Compile error: Expected End Function' (cursor indicates
error on second line).

It will compile if FactInterval is defined outside the Combination
function, but I would like to limit the scope of FactInterval.

Bo

Sun, 22 Dec 2002 03:00:00 GMT  Local function/sub inside function/sub

You can't do it - make it a separate function. A 'factorial' function
is pretty useful on its own anyway.

Steve.

Quote:
> How do I define a local function (or sub) inside a function or sub?
> I want to define a function locally as it is only used within the
> outer function. My example code looks like this:
> Function Combination(n As Integer, r As Integer) As Double
>   Function FactInterval(First As Integer, Last As Integer) As Double
>     Dim f As Double
>     Dim i As Integer

>     f = 1
>     For i = First To Last
>       f = f * i
>     Next i
>     FactInterval = f
>   End Function

>   Combination = FactInterval(n - r + 1, n) / FactInterval(1, r)
> End Function
> VB5 gives me 'Compile error: Expected End Function' (cursor indicates
> error on second line).
> It will compile if FactInterval is defined outside the Combination
> function, but I would like to limit the scope of FactInterval.
> Bo

Mon, 23 Dec 2002 03:00:00 GMT  Local function/sub inside function/sub

Fri, 19 Jun 1992 00:00:00 GMT  Local function/sub inside function/sub
VB isn't structured in that way - what you should do is to make the
function private to the same module (or class, form or whatever) in
which you're working:

MODULE: mdlSomething
--------------------

Option Explicit

Private Function FactInterval(First As Integer, Last As Integer) As
Double
Dim f As Double
Dim i As Integer

f = 1
For i = First To Last
f = f * i
Next i
FactInterval = f
End Function

Public Function Combination(n As Integer, r As Integer) As Double
Combination = FactInterval(n - r + 1, n) / FactInterval(1, r)
End Function

Quote:
-----Original Message-----

Posted At: Thursday, July 06, 2000 1:51 AM
Posted To: syntax
Conversation: Local function/sub inside function/sub
Subject: Re: Local function/sub inside function/sub

You can't do it - make it a separate function. A 'factorial' function
is pretty useful on its own anyway.

Steve.

> How do I define a local function (or sub) inside a function or sub?

> I want to define a function locally as it is only used within the
> outer function. My example code looks like this:

> Function Combination(n As Integer, r As Integer) As Double

>   Function FactInterval(First As Integer, Last As Integer) As Double
>     Dim f As Double
>     Dim i As Integer

>     f = 1
>     For i = First To Last
>       f = f * i
>     Next i
>     FactInterval = f
>   End Function

>   Combination = FactInterval(n - r + 1, n) / FactInterval(1, r)
> End Function

> VB5 gives me 'Compile error: Expected End Function' (cursor indicates
> error on second line).

> It will compile if FactInterval is defined outside the Combination
> function, but I would like to limit the scope of FactInterval.