Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



How to create Functions in VBScript?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 29-01-2010
Member
 
Join Date: Jul 2006
Posts: 283
How to create Functions in VBScript?

Hi friends,
I am new to the scripting. I have done C, C++ and VB. But I am totally new for the scripting part. I have started recently with the VBScript. Can anyone explain me how to create functions in VBScript? Because I know that using the function will definitely decrease my coding lines. So I am very eager to create the functions. Please help me by providing some sample of coding for creating the function in VBScript.!!
Reply With Quote
  #2  
Old 29-01-2010
Member
 
Join Date: Nov 2008
Posts: 1,193
Re: How to create Functions in VBScript?

You are absolutely right, that using the function will reduce your coding lines in the program. When you write the exact code, functions are also useful to increase code readability. all the programs that you write will benefit from functions. Whether you use the pre-made functions like document.write() or make your own it is a necessity for any programmer to have a basic understanding of how functions work in VBScript. So it will be better to use the functions in VBScript.
Reply With Quote
  #3  
Old 29-01-2010
Member
 
Join Date: Nov 2008
Posts: 997
Re: How to create Functions in VBScript?

Since you are new in VBScript, lets use some simple function for an example. The function myAdd will take two numbers and print out the result. The following is the code in VBScript for that example :
Code:
<script type="text/vbscript">
Function myAdd(x,y)
    myAdd = x + y
End Function
'Let's use our first function!
Dim result
result = myAdd(12,18)
document.write(result)
</script>
After using the above code for the myadd function, you will get the following output :
Code:
30
Reply With Quote
  #4  
Old 29-01-2010
Warner's Avatar
Member
 
Join Date: Mar 2008
Posts: 349
Re: How to create Functions in VBScript?

The Function and End Function keywords defines the function in VBScript. The following is the method used for creating VBScript functions :
  1. Start by using the Function keyword. by using this the browser knows that the function is defined.
  2. Then provide the name for the function.
  3. Use the opening and closing brackets after writing the name.
  4. Add the names of any arguments that the function requires.
  5. Write the code that makes up the function, which should be started on the new line.
  6. Lastly finish with the End Function.
The following example will describe it :
Code:
Function add(number1,number2)
  add = number1 + number2
End Function
Reply With Quote
  #5  
Old 29-01-2010
Solitario's Avatar
Member
 
Join Date: Aug 2006
Posts: 220
Re: How to create Functions in VBScript?

The following example demonstrates the creating and calling a VBScript function.
Code:
<script type="text/vbscript">
Function add(number1,number2)
  add = number1 + number2
End Function
Dim total
total = add(15,10)
document.write(total)
</script>
Using the above code, the following output will be produced:
Code:
25
__________________
I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create Functions in VBScript?"
Thread Thread Starter Forum Replies Last Post
How to create factorial of numbers using sub procedure in vbscript Gurseerat Software Development 1 1 Week Ago 12:21 PM
How to create ZIP Files from Command-Line via VBScript? FB Windows Server Help 10 22-12-2011 12:18 AM
How to create procedures in VBScript Betelgeuse Software Development 4 30-11-2010 01:45 AM
Vbscript to create sites on a windows 2008 DC Dipti Seth Active Directory 2 29-05-2010 06:14 PM
how to create automatic self extracting zip file in vbscript vivekmohan Software Development 1 03-09-2009 10:10 AM


All times are GMT +5.5. The time now is 03:46 AM.