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!

Function not returning value

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I am writing a calendar and it works roughly as follows:

1. Build a string called FirstDay which is 01 & "/" & TheMonth & "/" & TheYear.

2. Work out what day of the week this is and set a value for a variable called counter

3. Pass the value of counter to a function that builds a string consisting of counter & "/" & TheMonth & "/" & TheYear

4. If the string created in step 3 is a valid date then display it.

My calendar was behaving a bit unusally so I stripped out all the code to the bear minimum and what I have found is that the function is called and does create the string 01/09/2008 but for some reason is not returning it to the main program. On the 2nd loop and onwards it does work however. Can anyone see where I'm going wrong?

<%
TheMonth=9
TheYear=2008

FirstDay="01" & "/" & TheMonth & "/" & TheYear
Select Case WeekDay(FirstDay)
Case 1
counter=2
Case 2
counter=1
Case 3
counter=0
Case 4
counter=-1
Case 5
counter=-2
Case 6
counter=-3
Case 7
counter=3
End Select

Select Case TheMonth
Case 1, 3, 5, 7, 8, 10, 12
DaysInMonth=31
Case 4, 6, 9, 11
DaysInMonth=30
Case 2
DaysInMonth=28
End Select

If TheMonth=2 And (TheYear/4)=Int(TheYear/4) Then DaysInMonth=29

Function GetEventDate(counter)
If counter>0 And counter<=DaysInMonth Then
eventdate=""
If counter<10 Then
eventdate=eventdate&"0"
End If
eventdate=eventdate&counter & "/"
If TheMonth<10 Then
eventdate=eventdate & "0"
End If
eventdate=eventdate & TheMonth & "/" & TheYear
Else
eventdate=""
End If
End Function

For i=1 To 31
Response.Write(counter) & ":"
GetEventDate(counter)
Response.Write(eventdate)
Response.Write("<br>")
counter=counter+1
Next
%>

Thanks very much

Ed
 
Further to my previous post I've tried a much smaller script and again it doesn't return a value for the 1st loop but after that it does:

<%
Function MultiplyNumbers(counter)
TheAnswer=counter*12
End Function

For i=1 to 12
counter=i
Response.Write(counter) & ":"
MultiplyNumbers(counter)
Response.Write(TheAnswer) & "<br>"
Next
%>

Am I missing something obvious?!

Thanks very much

Ed
 
I've figured this out now - it looks like I have to assign the variable a value of nothing before it can be used by the function - eg:

<%
Function MultiplyNumbers(counter)
TheAnswer=counter*12
End Function

For i=1 to 12
TheAnswer=""
counter=i
Response.Write(counter) & ":"
MultiplyNumbers(counter)
Response.Write(TheAnswer) & "<br>"
Next
%>


 
That succeeds only because TheAnswer is then of global scope. This is the canonical way to return value from function in case you've forgotten.
[tt]
Function MultiplyNumbers(counter)
[red]MultiplyNumerbs[/red]=counter*12
End Function

For i=1 to 12
[red]'[/red]TheAnswer=""
counter=i
Response.Write(counter) & ":"
[red]TheAnswer=[/red]MultiplyNumbers(counter)
Response.Write(TheAnswer) & "<br>"
Next
[/tt]
I wish I did not need to post this.
 
Typo here.
>[self] MultiplyNumerbs=counter*12
[tt] MultiplyNum[red]bers[/red]=counter*12[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top