SoFunction
Updated on 2025-04-08

VBS Programming Tutorial (Part )

Last article:

The new home is good, MM is happy, so I am happy too :) Today we will learn the last part of the basics: custom functions and procedures. We deal with functions every day. Inputbox() is a function, msgbox() is a function, and int() is also a function... These functions are built-in to the system, and we can only use it but not change it. Today, I will teach you how to make a function by yourself.

First of all, we need to understand why we need to use functions. We use "instances" to speak. Let's look at an example first: give two numbers and output the larger one.

dim a1,a2,b1,b2,c1,c2 
a1=2:a2=4 '":"Let you 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, we copied the same comparison process several times. When the early languages ​​were not structured (no procedures and functions), programmers did
It was done, they copied (Copy), and there was no clipboard in that era. Everyone re-entered the code. Later, the 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 usefunctionDefine a new function 
if t1>t2 then 
co=t1 'pass"Function name=expression"This method returns the result 
elseif t2>t1 then 
co=t2 
end if 
end function 

We are using a new keyword here: funciton, this keyword represents the beginning of a new function, format:
function Function name (parameter 1, parameter 2...parameter n) 'The list can be empty, but the brackets cannot be omitted. The parameters are divided by ","

... 
exit funciton 'end function, not required
... 
end function 

A function is a module that will only run when you call it. That is to say, when you write a function and then do not call it in the program, then this function
It will never run. Generally speaking, we write programs according to:

Main program
.. 
.. 
.. 

Function 1
.. 
.. 

Function 2
.. 
.. 

Explain in detail: The most important thing in a function is the parameters and return values. The parameters are defined in () after the function name. Use "," to split. When using parameters, we also

Use "," to divide. Speaking of this, I remembered something. Yesterday, a friend sent me a message and asked me:

msgbox(name1,name2,name3) 

What's wrong with this? Why can't three variables be displayed at the same time? This is because you used ",", this symbol indicates that the three quantities you entered as three different parameters.

Passing to the msgbox() function, the msgbox() function will only display the first parameter, and the function of the second parameter is to appear in the title bar. So you should use "&" or "+" to put

Three string variables are connected and passed to the msgbox() function as the first parameter. When programmers talk about parameters, they often talk about "formal parameters" and "real parameters" such as "black words",

Let me explain it. "Formal parameter" is the abbreviation of "formal parameter", "real parameter" is the abbreviation of "actual parameter". The actual parameter refers to the quantity passed to the function when you call it. It can

Make variables or constants (direct quantities), for example: 12 and 24 in co(12, 24) are real parameters. Formal parameters are variables you define when defining a function. These variables are used to "catch" and pass

The amount coming, for example, function co(t1,t2)t1, t2 is a formal parameter.

In VBScript, parameter transfer is a kind of value transmission, not address transmission (it doesn't matter if you don't understand, you will understand after learning the pointer of C language), so we have parameters

Number transfer is actually a variable assignment. For example, when we call co(a1, a2), the program will actually perform an operation such as t1=a1, t2=a2. Also because of the transmission

The reason for the value address is that VBScript can only return one value. Let’s first look at what “return” means. When one procedure calls another procedure (such as the main program calls

) The control authority reaches the called process. After the execution of this process, it will return to the place where it is called to continue execution. This is called "return", return

When you can bring a value called "return value" (this is a "common understanding". VBS inherits the tradition of basic, and when returning, the method of "function name = return value" is adopted.

This "return value" refers to an expression (in programming, anything is an expression, such as variable a, constant 0, "Hello", c=1+2, etc., which are all expressions). For example,

There is a function that is ht, and the method returned is: ht= the value you want to return. Note: After returning, the subsequent statement will no longer be executed.

I don't need to talk about calling a function: variable = function name (parameter)

Sometimes we don’t need to return any value. At this time, we can use a structure called "subprogram". The difference between subprograms or procedures and functions

It is: 1) There is no return value, 2) Use the sub keyword definition, 3) Call through Call. 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 in the basics") 
end sub

You must understand it, it's very simple. Exiting a process is the same as quitting a function: exit sub(function: exit function).

It should be noted that subprograms (processes) are relatively special structures. Languages ​​such as C do not have this concept. Everything in C is a function, and a function without a return value
In C language, just use the void modifier to define it.

There is nothing to talk about today, the basics are over. You have already developed basic programming concepts (process-oriented structured programming), and you can choose to learn
Another language (such as C or Pascal), the current foundation will be helpful. If you want to continue learning vbs or learn more about programming through it, you can continue to learn from me when you transform, but because my holiday is over, the update time may be slow. Please forgive me. The preliminary plan is as follows:


Advanced chapter:

In-depth discussion of variables

|----Variable Type

|-----Value Range of Variables

In-depth discussion of arrays

|---- Dynamic Array

In-depth discussion of functions

|-----Array as function parameters

|-----Multiple return values

String operation

|-----A lot of messes

Basic knowledge of object-oriented programming (OOP)

File Operation

|----FSO object

|----Other related parts

VBS and web pages

|----Embed VBS in HTML

|----VBS and forms (design your program interface wow!)


Practical combat:

Virus Programming

Socket Programming (TCP/UDP)

This is just a general content. I think there should be changes. You will read it at that time. Please practice more today's content. The homework is to review the previous courses. For friends who want to leave this course to learn further: I wish you a smooth road to learning programming.