April 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Discovering Visual Basic .NET: Using Functions and Arguments

  • December 30, 2004
  • By Bill Hatfield
  • Send Email »
  • More Articles »

In the previous article in this series, you discovered how to use variables to store information, do math and other operations, and how to display the results. In this article, you'll explore the use of functions and arguments.

Commands in VB.NET come in two flavors: statements and functions. A statement is a command that stands on its own and simply does something. Dim, which declares variables, is a statement:

Dim Artist, CD As String

A function, on the other hand, returns a value for you to use in your code. In this article, I describe how functions work and introduce you to some functions that are handy when dealing with dates.

Getting a Date

The following code shows how you can use the Today function:

Imports System
Imports Microsoft.VisualBasic

Module DateToday
  Public Sub Main()
    Dim CurrentDate As String
    CurrentDate = Today
    Console.WriteLine("The current date is " & CurrentDate)
  End Sub
End Module

The first thing you'll notice is that I added another Imports line at the top. The Microsoft.VisualBasic library is one that contains all the core functions for the Visual Basic language. You'll want to include this library in almost every Visual Basic .NET application that you write.

The variable CurrentDate is created and then assigned the value that is returned from the Today function. Then, the value of CurrentDate is used within a sentence to inform the user of the current date.

You might think that Today looks like a variable that simply isn't declared—And you're right. It does look like that. However, you know it isn't a variable because it isn't declared and because you happen to know that Today is a built-in function with a special meaning in VB.NET. (Okay, you may not have known that before, but you do now!) You'll discover more built-in functions as you explore VB.NET.

What Is Meant By "Returns a Value?"

If you haven't used a computer language with functions before, the concept of a function returning a value can be confusing. When I say a value is returned, what do I mean?

You call the built-in VB.NET function Today when you use its name in the code. When it's called, the function goes out to the system clock and finds out the current date. Then, the function sends that current date back to your code and places the value in the code right where the Today function appears. The date returned by the function is then assigned to the CurrentDate variable:

CurrentDate = Today

So, this line does two things:

  • It calls the Today function.
  • It assigns the date returned from the Today function to the CurrentDate variable.

Understanding Arguments...

A function also can have arguments (sometimes referred to as parameters). An argument is a value that you can send to the function when you call it. By passing an argument to a function, you give the function information to work on. For example, take a look at the Weekday function:

Imports System
Imports Microsoft.VisualBasic

Module WeekdayToday
  Public Sub Main()
    Dim ThisDay, ThisDate As String
    ThisDate = Today
    ThisDay = Weekday(ThisDate)
    Console.WriteLine("It is day number " & ThisDay)
  End Sub
End Module

First, ThisDay and ThisDate are declared as string variables.

The next line calls the Today function. The Today function gets the system date and returns it. The value returned is assigned to ThisDate.

The next line calls the Weekday function and passes the value of ThisDate as an argument. Weekday uses this date to determine the weekday on which the date falls. Notice that you pass arguments by placing them after the function name and putting them in parentheses. If this function had taken two arguments, you would put them both inside parentheses and separate them with a comma.

For the Weekday function, the value returned is a number from 1 to 7, indicating Sunday through Saturday. If you want to make this code more concise, you could do it this way:

Imports System
Imports Microsoft.VisualBasic

Module WeekdayToday
  Public Sub Main()
    Dim ThisDay As String
    ThisDay = Weekday(Today)
    Console.WriteLine("It is day number " & ThisDay)
  End Sub
End Module

The Today function is called first and then the value it returns is immediately sent to the Weekday function as an argument.





Page 1 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel