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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating functions 1

Status
Not open for further replies.

spiderman0009

Technical User
Feb 19, 2002
23
CA
Hello

I am not sure I understand how this part in " " means below...

Creating functions

A function, as you may expect, works like a VBScript function - it returns a value, and when it is called it can be included as part of a formula.

Function CalculateCost(FoodCost, HotelCost, HourlyRate)
Dim TotalCost
...
CalculateCost = TotalCost
End Function

Just like a subroutine, the Function identifies the name of the function and the arguments. End Function comes at the end. The name of the function should be assigned a value sometime before the End Function. This value assigned to the name of the functions is the value that is sent back to the calling routine. "In the preceding code, a variable, TotalCost, is used in all the calculations and then it is assigned to CalculateCost, the name of the function just before the function ends."

Is there a simpler way to put this ... so that a sesame street person would be able to understand?

Can you help>>>???
 
Functions have to start with "function" and end with "end function"

Funtions return a value back to you, for example you could create a function to calculate VAT on a number. So you pass a value to the function, say 100, and it returns the value 117.5 (adds 17.5%).

This is done by calling the function in your code like this:

numberplusvat = vatfunction(100)

which will make numberplusvat equal to 117.5

The number in the brackets is called a parameter, and it is sent to the function.

The function has to have a name.

When you give it this name, a variable is created automatically called the name of your function.

Any parameters which are passed to your function have to be declared in brackets at the start of the function.

So your first line of the function could be:

function vatfunction(valuepassed)

You can see that the bit in bold mirrors the way we are calling the function from the code

When you pass 100 to the function it puts 100 in the variable called "valuepassed" in our example, and creates another variable called "vatfunction". Whatever the value of "vatfunction" is at the end of the function will be the value returned.

So:

function vatfunction(valuepassed)
vatfunction = valuepassed * 1.175
end function

will be the finished product. See how we have made "vatfunction" the new value, and we haven't declared it or anything. If the line said:

vatfunction = "pants"

then the result of the line

numberplusvat = vatfunction(100)

would make the variable numberplusvat equal to "pants"

Hope this is more helpful (if somewhat longer!)

Derren
[Mediocre talent - spread really thin]
 
Question??? What is VAT stand for? I am really new to this VBScripting.
 
HI Man

Thanks... Could you possible tell me what the code means in the original example "CalculateCost = TotalCost"

Don't understand what they are doing this for?

Could you also give me this example with all the numbers in it>.. I think that might help me to fully understand.

Thank again
Troy

 
All a function does is encapsulate a bit of code which is used frequently and returns the result. In my case it calculates the Value Added Tax on the price of goods, which is 17.5%.

I send my value to the function, which does its thing and then sets the variable "numberplusvat" to the value that I want returning. This is what is happening in the MS example. The name of their function is called "calculatecost" so the last line "calculatecost = totalcost" is setting the return value for the function.

The most relevant way to deal with this is for you to tell me what function you are trying to do, or an example which means something to you and we can go through it some more. Derren
[Mediocre talent - spread really thin]
 
What if we wanted for example to calcualte tax on my pants.

function pants($15)
pants = $15 * 1.07
end function

IS this right? Is this how you would do it?
 
That is absolutely the correct way to view functions. However, in order to reuse the function we write them so that they will do their stuff on ANY number we wish to throw at them.

So we use "parameters", which are just variables to replace the "$15" in this case. The function will look like this:

function pants(price) ' where price is the parameter
pants = price * 1.07 ' we can then call price as the value
end function

Now any value we send to the funtion will be converted to the price plus tax. It is completely reuseable.

If this was written in your page, in with the rest of your vbscript, and you suddenly had the need to find out the pants plus tax and write it to the page you would just do something like this:

' add the tax by calling the function with the value
mypantscost = pants(15)
response.write("My pants cost $" & mypantscost)

So that is how you would call your function. Note here as well that "response.write()" is also a function, we just tell it what to write within the brackets.

Good luck Derren
[Mediocre talent - spread really thin]
 
Thank You
I am so happy... to get it.. I also had to make a variable called mypantscost


<% function pants(price) ' where price is the parameter
pants = price * 1.07 ' we can then call price as the value

end function %>
<% Dim mypantscost
mypantscost = pants(15)
response.write(&quot;My pants cost $&quot; & mypantscost) %>

How can you make this work so that someone could imput the text online and then it would calculate the tax by hitting the submit button..

Your awsome... Thank you very much!!!
 
When the form is submitted to an asp page each named form field becomes a member of the request collection of values. These can be referred to using

request.form(&quot;pants&quot;)

where pants was the name of the text box/form field.

If you have your function in your page you can simply do this to use the function:

dim pantsvalue
pantsvalue = pants(request.form(&quot;pants&quot;))

Now pantsvalue is the price of the pants plus tax.

Glad to hear that you find this helpful. If you think anyone else could benefit from the thread mark it as helpful. Derren
[Mediocre talent - spread really thin]
 
Next question sounds dumb... But how do you submit a form to an asp page?

Thank Ypu
Troy
 
It's not stupid!

Your form needs to have an action associated with it, and you need to set this to the location of your asp page which will process it.

This goes in the &quot;form&quot; html tag :

Code:
<form name=&quot;yourform&quot; method=&quot;post&quot; action=&quot;yourasppage.asp&quot;>

If you then have a submit button on the form and it is pressed the values within the form are sent to the asp page for processing. You can then refer to the as request.form(&quot;fieldname&quot;) in the asp page.

Small sidenote: the &quot;method&quot; can be GET or POST. &quot;GET&quot; passes the value of the form as querystrings appended to the end of the url and these can be referenced using request.querystring(&quot;fieldname&quot;). Post is the &quot;hidden&quot; way with no length restrictions and is referenced using request.form(&quot;fieldname&quot;) - this is the best method.

However, you can use just request(&quot;fieldname&quot;) and it will pick up either. Derren
[Mediocre talent - spread really thin]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top