VBS BASIC Programming Tutorial (6th) _vbs

Source: Internet
Author: User
Last article:

New home is good, mm happy, I will follow the happy: Today we learn the last part of the basic article: custom functions and procedures. We are in and letters every day

Number, InputBox () is a function, MsgBox () is a function, int () is also a function ... These functions are built in the system, we can only use can not change. Today, I

Teach you how to make a function yourself.

First we need to understand why we use the function, we use "example" to speak, first look at an example: give two numbers, the output of the larger one.

Dim a1,a2,b1,b2,c1,c2
A1=2:a2=4 ': ' Allows you to write multiple statements on one line
b1=32:b2=67
c1=12:c2=898

If A1>a2 Then
MsgBox (A1)
ElseIf A1
MsgBox (A2)
End If

If B1>b2 Then
MsgBox (B1)
ElseIf B1
MsgBox (B2)
End If

If C1>c2 Then
MsgBox (C1)
ElseIf C1
MsgBox (C2)
End If

How troublesome it is that we have replicated the same comparison process several times, when the early language was unstructured (without processes and functions), and programmers did

dry, they copy (copy), there is no clipboard in that era, we all re-enter the code. Later work was simplified:


Dim a1,a2,b1,b2,c1,c2
A1=2:a2=4
b1=32:b2=67
c1=12:c2=898
MsgBox (Co (A1,A2))
MsgBox (Co (B1,B2))
MsgBox (Co (C1,C2))

function Co (T1,T2) ' We defined a new functions using function
If T1>t2 Then
Co=t1 ' Returns the result in this way through the function name = Expression
ElseIf T2>t1 Then
Co=t2
End If
End Function

Here we are using a new keyword: funciton, this keyword represents the beginning of a new function, format:

Funciton function name (parameter 1, parameter 2 ...) Parameter n) ' list can be empty, but parentheses cannot be omitted, and arguments are divided between ","
...
Exit Funciton ' End Function, not required
...
End Function

A function is a module that runs only when you call it, and it says that when you write a function and then do not call it in a program, the function

Will never run. In general, we write programs in accordance with:

Main program
..
..
..

Function 1
..
..

Function 2
..
..

To explain in detail: the most important thing in a function is the parameter and the return value. Parameters are defined in the () after the function name, separated by "," and we Also

Use "," split. Speaking of which I remember one thing, yesterday a friend sent me a message to ask me:

MsgBox (Name1,name2,name3)

Where is this mistake? Why not display three variables at the same time? This is because you used the word ",", which indicates that you entered three quantities as three different parameters.

Passed to the MsgBox () function, the MsgBox () function only displays the first argument, and the second argument appears in the title bar. So you should use the "&" or "+" to

Three string variables are concatenated and passed as the first argument to the MsgBox () function. Programmers say parameters often speak of "formal parameter", "real" such as "slang",

Let me explain. "Formal parameters" is the abbreviation of "formal parameter", "argument" is the abbreviation of "actual parameter", the argument refers to the quantity that you pass to the function when you call the function, you can

Make a variable or a constant (a direct amount), for example, a 12,24 in Co (12,24) is an argument. A parameter is a variable that you define when you define a function, which is used to "catch" the passing

The amount that comes over, such as function Co (T1,T2) T1,T2 is the formal parameter.

In VBScript, parameter passing is a value, not an address (do not understand it does not matter, learn the C language pointer you understand), so we carry on the participation

The number pass is actually a variable assignment, such as we call Co (A1,A2), and in fact the program does one step: t1=a1,t2=a2 such an operation. Also because the biography

For the reason that VBScript can only return a value, let's look at what is called "return." When a procedure calls another procedure (for example, the main program calls the

function), the control goes to the called process, and when the process is finished, it goes back to where it was called, which is called "return" and returns

Can take a value called "Return value" (This is "popular" understanding). When the VBS inherits basic tradition, it returns with the "function name = return value" approach,

This "return value" means an expression (in programming, everything is an expression, such as variable A, constant 0, "Hello", c=1+2, etc.). Like what

If one of the functions is HT, the return method is: ht= The value you want to return. Note: After returning, the following statements will no longer execute.

Call a function I don't have to tell you this: variable = function name (parameter)

Sometimes we don't need to return any values, and we can use a structure called "subroutines" at this time. A subroutine or a difference between a procedure and a function.

is: 1 There is no return value, 2 is called using the Sub keyword definition, 3. With an example:

Dim yname
Name=inputbox ("Please enter your name:")
Call Who (Yname)

Sub-who (CNAME)
MsgBox ("Hello" & CNAME)
MsgBox ("Thank you for reading my course")
MsgBox ("This is the last lesson of the basic section")
End Sub

You must have read it, it's very simple. Quit a procedure and quit a function: Exit Sub (function: Exit Function).

Note that subroutine (procedure) is a special structure, C and other languages do not have this concept, C language is all functions, no return value of the function

Just use the void modifier definition in the C language.

There is nothing to talk about today, the basic article is so over, you already have BASIC programming concept (process-oriented structured programming), you can choose to learn

Another language, such as C or Pascal, is now based on a certain amount of help. If you want to continue to learn the VBS or through it more detailed understanding of programming in transition can

Continue to study with me, but because my holiday is over so the update time may be slow, please forgive me. The initial plan is as follows:


Advanced Chapter:

An in-depth discussion of variables

|----Variable Type

The effective range of----variables

An in-depth discussion of arrays

|----Dynamic Array

An in-depth discussion of functions

|----array as function parameter

|----multiple return values

String manipulation

|----a lot of mess.

Basic knowledge of object-oriented programming (OOP)

File actions

|----FSO Object

|----Other Related parts

VBS and Web page

| Embed a VBS in----HTML

|----VBS and FORM (Design your Program Interface wow!)


Practical article:

Virus programming

Socket Programming (TCP/UDP)


This is just about the content, I think there should be changes, then you will read. Today's content please a lot of practice, homework is to put the previous courses in the temperature

Learning. For those who want to leave the course to further study the friend: I wish you learn programming on the road smooth sailing.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.