<< Chapter < Page Chapter >> Page >

[As<KIỂU DỮ LIỆU>]

<Các dòng lệnh>hay<Các khai báo>

End Function

Trong đó:

-<Tên hàm>: Đây là một tên được đặt giống quy tắc tên biến, hằng,…

-<tham số>[:<Kiểu tham số>]: có thể có hay không? Nếu có nhiều tham số thì mỗi tham số phân cách nhau dấu phẩy. Nếu không xác định kiểu tham số thì tham số có kiểu Variant.

-<KIỂU DỮ LIỆU>: Kết quả trả về của hàm, trong trường hợp không khai báo As<kiểu dữ liệu>, mặc định, VB hiểu kiểu trả về kiểu Variant.

Khi gọi hàm để thực thi ta nhận được một kết quả. Cần chú ý khi gọi hàm thực thi ta nhận được một kết quả có kiểu chính là kiểu trả về của hàm (hay là kiểu Variant nếu ta không chỉ rõ kiểu trả về trong định nghĩa hàm). Do đó lời gọi hàm phải là thành phần của một biểu thức.

Cú pháp gọi hàm thực thi:<Tên hàm>[(tham số)].

Ví dụ: Tính N!

  • TextBox: Name:txtNum Bước 1: Thiết kế chương trình có giao diện:

Label: Name: lblKQ

  • Bước 2: Thêm một hàm vào cửa sổ mã lệnh của Form

Function Giaithua(N As Integer) As Long

Dim i As Integer, Kq As Long

Kq = 1

For i = 1 To n

Kq = Kq * i

Next

Giaithua = Kq

End Function

Private Sub Command1_Click()

Dim n As Integer

n = Val(txtNum.Text)

lblKQ.Caption = Str(Giaithua(n))

End Sub

Lưu dự án và chạy chương trình ta được kết quả như hình dưới:

Lưu ý: Do khi gọi hàm ta nhận được một kết quả nên bên trong phần định nghĩa hàm, trước khi kết thúc ta phải gán kết quả trả về của hàm thông qua tên hàm (trong ví dụ trên là dòng lệnh Giaithua = Kq)

Truy xuất dữ liệu trong Visual Basic

Các khái niệm

  • Module:

- Một ứng dụng đơn giản có thể chỉ có một biểu mẫu, lúc đó tất cả mã lệnh của ứng dụng đó được đặt trong cửa sổ mã lệnh của biểu mẫu đó (gọi là Form Module). Khi ứng dụng được phát triển lớn lên, chúng ta có thể có thêm một số biểu mẫu nữa và lúc này khả năng lặp đi lặp lại nhiều lần của một đoạn mã lệnh trong nhiều biểu mẫu khác nhau là rất lớn.

- Để tránh việc lặp đi lặp lại trên, ta tạo ra một Module riêng rẽ chứa các chương trình con được dùng chung. Visual Basic cho phép 3 loại Module:

Module biểu mẫu (Form module): đi kèm với mỗi một biểu mẫu là một module của biểu mẫu đó để chứa mã lệnh của biểu mẫu này. Với mỗi điều khiển trên biểu mẫu, module biểu mẫu chứa các chương trình con và chúng sẵn sàng được thực thi để đáp ứng lại các sự kiện mà người sử dụng ứng dụng tác động trên điều khiển. Module biểu mẫu được lưu trong máy tính dưới dạng các tập tin có đuôi là *.frm.

Module chuẩn (Standard module): Mã lệnh không thuộc về bất cứ một biểu mẫu hay một điều khiển nào sẽ được đặt trong một module đặc biệt gọi là module chuẩn (được lưu với đuôi *.bas). Các chương trình con được lặp đi lặp lại để đáp ứng các sự kiện khác nhau của các điều khiển khác nhau thường được đặt trong module chuẩn.

Module lớp (Class module): được sử dụng để tạo các điều khiển được gọi thực thi trong một ứng dụng cụ thể. Một module chuẩn chỉ chứa mã lệnh nhưng module lớp chứa cả mã lệnh và dữ liệu, chúng có thể được coi là các điều khiển do người lập trình tạo ra (được lưu với đuôi *.cls).

  • Phạm vi (scope): xác định số lượng chương trình có thể truy xuất một biến. Một biến sẽ thuộc một trong 3 loại phạm vi:

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask