Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Leuven, Vlaanderen, Belgium
    Posts
    322
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Good coding proc. parameter & intermediate value (All)

    Hey,

    In my quest for writing better code, as I'm no real programmer, I didn't find the exact information about the following... In case the answer on either of these questions is 'yes', I'll be very glad with any suggestion, link to a thread, site or white paper where this is described properly...

    1. Suppose you have a procedure fed with one or more parameters. Are there any disadvantages if I use these parameters directly in code, instead of assigning them first to explicitly declared variables? I figured out that you loose performance when you don't do this with form controls/fields, but I guess chances are small that this applies to parameters too.
    2. Are there any disadvantages if I use the function name also in intermediate steps during the procedure, instead of using variables as long as possible, and the function only in the last step?

    For example:
    Public Function function1(strName as String) as String
    function1 = function2(strDirName)
    function1 = LCase(function1)
    End Function

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Good coding proc. parameter & intermediate value (All)

    When you pass some data to a procedure as an argument it is a variable. If passed by reference (the default in VB/VBA) the variable points back to the variable in the calling procedure. If passed by value (ByVal) the variable is passed as a copy of the variable. There is no need to turn around and assign this value to another variable unless there is a reason to do so. If you don't want the called procedure to change the value of the variable referenced in the calling procedure, pass the argument by value.

    As for assiging values to Function names, according to VB Help for Function Statement: "To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure." In other words there's no rule that says you can only assign a value to a function name once, at end of the procedure.

    For more info, for a start, recommend open VB Help, select "Contents" pane and then check out "Visual Basic Conceptual Topics" topics, these may be useful in explaining how some of these concepts work in VB/VBA. Also recommend check out "Visual Basic Language Reference" topics (also under Contents), under "Statements" look up "Sub Statement", "Function Statement", and related topics. Not that VB Help Files are the most coherent reference available, but may serve as an good introduction to these topics.

    HTH

  3. #3
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Leuven, Vlaanderen, Belgium
    Posts
    322
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Re: Good coding proc. parameter & intermediate value (All)

    Thanks Mark... you told me all I needed to know <img src=/S/bow.gif border=0 alt=bow width=15 height=15>.
    And I'll check out these help pages too. I seem to have skipped them a bit too easily ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •