Home
Username:
Password:
Microsoft Visual Basic .NET Tutorials

Functions / Creating a Function




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Now that we've defined a function, let's go take a look at a function and see how to build one, how to invoke it and how it behaves. So let's go again back out to visual studio .NET. And let's start off by going into our class test and creating a very simple function, ok, and I'm going to call this Public function and I'm going to call it Janet. I'm not going to pass anything in, then I'm going to say as integer 16, now this is telling me that this function Janet is not going to take anything in but it's going to pass something back as an integer and so now I'm just going to say return 42. And so when I call this it's just going to bounce 42 back. Up here in my sub main I'm going to create an instance of an object aa As New Test, again because it's the name of my class where my functions lives and then I'm going to call aa.Janet(). Now if I execute this, it will simply go down and return 42. But I won't be able to see it because I don't really have this anywhere so what I'm going to do is do a console write line and then call my function Janet. So now when I run this it will do a console write line when it hits this, it will jump down run the function Janet, return 42 back and put it in this place, and print that for me. So I'm going to build my executable and then I'll go run it. So execute it by saying class test, and notice 42 came back so that's exactly what it did. Now let's make it a little more interesting, let's say that I want to pass some things into this. So let's delete this up here first, ok. And now I'm going to say that I'm going to pass ByVal a As Int16, and b, and ByVal b As Int16. Now I'm passing those in, and then inside this function I'm going to - Dim Private c As Int16 - and then, why don't we do this, I don't have to do private there because I'm inside a function. And so now I'm going to say - c = a+b - and now I'm going to - Return c. And so now when I call Janet, I will say console write line, and I'll call Janet and this time I will pass Janet 5+11, OK so 5 and 11 that's what I'm passing down. So what's going to happen this time is I'm going to pass in 5 and 11. Janet will get it, 5 will go in as a, b will go in as 11, we'll add these two together c will be equal to 16 and I will return 16 and print that out. So let's see if that's what happens here. So I'm going to start run command. Type class test, and notice 16 came back, so that's exactly what my function did. Now this is just a simple example of what functions can do, and you'll notice that rather than just carrying out some action, functions actually return information to me. You'll also notice here that functions, notice Janet's a function Mark is a subroutine, notice right here Mark is a sub and down here Janet is a function. But when I call these up in my object they both appear as methods. That's the way they work.

Tutorial Information

Course: Microsoft Visual Basic .NET
Author: Mark Long
SKU: 33433
ISBN: 1932072349
Release Date: 2003-05-27
Duration: 6 hrs / 87 lessons
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available