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.

Thanks in advance,
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.
> Thanks in advance,
> 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.

> Thanks in advance,
> Bo



Fri, 27 Dec 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Calling a sub or function using a variable through another sub or function

2. Returning sub or function name within a specific sub or function

3. HOWTO: Call JavaScript function from inside a VBScript Sub

4. execute a variable as a function/sub procedure (like eval function in java(script))

5. menus and sub-menus and sub-sub-menus

6. count elements in root, sub root sub-sub root treeview

7. Obtaining Current Sub/Function Name

8. Subs vs Functions

9. Exit Sub from called Function

10. Passing Value from Module Function to Form sub problem

11. Who called this sub or function?

12. Sub/Function not defined

 

 
Powered by phpBB® Forum Software