Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning value from function 1

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I've been programming for some time now and sadly I am still baffled by functions. Sometimes they do what I excpect and other times not.

I am trying to write a function that gets rid of any illegal characters from a folder that is to be created.

Code:
<%
Function CleanFolderName(TheFolder)
  TheFolder=Replace(TheFolder & "", "/", "-")
  TheFolder=Replace(TheFolder & "", ":", "-")
  TheFolder=Replace(TheFolder & "", "*", "-")
  TheFolder=Replace(TheFolder & "", "?", "-")
  TheFolder=Replace(TheFolder & "", """", "-")
  TheFolder=Replace(TheFolder & "", "<", "-")
  TheFolder=Replace(TheFolder & "", ">", "-")
  TheFolder=Replace(TheFolder & "", "|", "-")
End Function

TheFolder="\intranets\bwbnet\docs\root\Charity Dept Admin*"
TheFolder=CleanFolderName(TheFolder)
Response.Write(TheFolder)
%>

For some reason which I cannot fathom my line response.write(TheFolder) does not display anything. Is it something to with variables being public or private?

Thanks

Ed
 
[tt]Function CleanFolderName(TheFolder)
'etc etc
CleanFolderName=TheFolder
End Function
[/tt]
ps: For what reason this is not asked in asp forum?
 
There are 2 ways to return data from a function/subroutine. Functions are designed to return a single piece of data similar to the way tsuji showed. Additionally, you can use the parameter of the function or subroutine to return data.

The parameter method can seem a little complicated until you get used to it. There are actually two types of parameters, ByRef and ByVal. The default behavior is ByRef. When you pass a parameter By Reference (ByRef), you are passing a pointer to the variable. When you make changes to that variable inside the subroutine, the data is also changed outside the subroutine. You can also pass a variable By Value (ByVal). When you do this, the value of the variable is passed in. Whatever changes you make to the variable inside the subroutine are lost after the subroutine ends.

subroutine
Take a look at the following subroutines:

Code:
<%
    Sub ByRefTest([!]ByRef[/!] Val)
        Val = "Hello World"
        Response.Write "ByRef Test Inside: " & Val & "<br/>"
    End Sub

    Sub ByValTest([!]ByVal[/!] Val)
        Val = "Goodbye World"
        Response.Write "ByVal Test Inside: " & Val & "<br/>"
    End Sub
%>

If you don't use ByRef or ByVal, the default behavior is ByRef. Both functions will change the value of the parameter within the function, and the respose.write lines will do exactly as you expect them to. Now consider this code that call the functions.

Code:
<% 

  Greeting = "Hello Joe"
  Response.Write "ByRef Test Before: " & Greeting & "<br/>"
  Call ByRefTest(Greeting)
  Response.Write "ByRef Test After: " & Greeting & "<br/>"

  Response.Write "<br/>"

  Greeting = "Goodbye Joe"
  Response.Write "ByVal Test Before: " & Greeting & "<br/>"
  Call ByValTest(Greeting)
  Response.Write "ByVal Test After: " & Greeting & "<br/>"

%>

If you run this code, you will see the following output:

[tt][blue]
ByRef Test Before: Hello Joe
ByRef Test Inside: Hello World
ByRef Test After: Hello World

ByVal Test Before: Goodbye Joe
ByVal Test Inside: Goodbye World
ByVal Test After: Goodbye Joe
[/blue][/tt]

Notice that the ByRef function changed the value of the variable inside the subroutine and afterwards, the value is still changed. The ByVal subroutine also changes the value inside the subroutine, but the changes are lost when you exit the subroutine.

function

You can use the parameter method of returning values from a function, but to set the actual return value, you need to set the name of the function to the value you want returned.

Ex:

Code:
  Function TestOutput
    TestOutput = "Hello World"
  End Function


Now, let's take a look at your function.

Code:
Function CleanFolderName(TheFolder)
  TheFolder=Replace(TheFolder & "", "/", "-")
  TheFolder=Replace(TheFolder & "", ":", "-")
  TheFolder=Replace(TheFolder & "", "*", "-")
  TheFolder=Replace(TheFolder & "", "?", "-")
  TheFolder=Replace(TheFolder & "", """", "-")
  TheFolder=Replace(TheFolder & "", "<", "-")
  TheFolder=Replace(TheFolder & "", ">", "-")
  TheFolder=Replace(TheFolder & "", "|", "-")
End Function

You are not setting the output value of your function. Now, let's look at the way you call your function.

Code:
TheFolder=CleanFolderName(TheFolder)

You don't specify the parameter type, so it defaults to ByRef. If you did...

Call CleanFolderName(TheFolder)

You function would have worked for you because the value of the parameter is modified inside the function. But, since you are not setting the value of the function, you get an empty string returned. Since you the set your 'TheFolder' variable to the output (which is nothing), you lost your results.

I know this may seem a little long-winded, but this is really important stuff. If you don't understand something, let me know and I will explain further.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hi George,

Thanks for this that is very kind and extremely helpful - there's quite a bit to think about there so I'll code up the examples you've given me so I can see first hand how they behave.

Thanks again - this will revolutionise things for me!

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top