Subroutines and Functions

Well I am writing this tutorial in which i wil tell you about subroutines and functions in vb.net

Note: This tutorial can also apply for other languages because the basic programming fundamentals are the same .

 

And you can also take a look at this page in which this concept is explained clearly – http://www.homeandlearn.co.uk/net/nets9p1.html

So first start from the easiest concept.

We all know that in our form class there are many subroutines like

a subroutinethat appears when we double click an command button in design view.

That is a subroutine because of the keyword sub which means it is a subroutines.

Subroutines and Functions are called procedures .

Remember procedures are jsut self contained blocks of code and are of 2 types

1. Subroutines

2. Functions

Now let's see the procedure(subroutine) that appears in code view when you double click an button in desig view.

In this screenshot errorcheck is a subroutine.

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

This is a self contained block of code which means it contains code inside itself (in this case it dosnt because we havnt enter anything inside it)

So the first time you look at it you can say it is a self contained block of code it contains code inside

sub statements and end sub statement

Now we also see that sub keyword in the first line which obvyously means it is a subroutine .

So now comes the time when you should know the defination of subroutine .

A subroutine is a procedure which does some thing(someprogramming task) but it dosnt return any value.

example : the command1_click sub

we can call subroutines by just entering their name like take the example of this sub.

public sub dosomething()

endsub

Now to call it simple type in

dosomething()

 

And Now let's discuss about functions .

Functions are same as subroutines . They also perfom some task but they have a special property that they return values inside the calling variable .

verify postal code is a function as it returns an string value to a variable that calls it

confusing?? Dont worry

i will show you what i mean

when you have written this function and you know that a function is a procedure that returns a value then you have to store this value somewhere when you call it

so in this case it returns a string value so we will declare a new variable as string

dim test as new string

now we will calll the function

test=verifypostalcoe(arguments )

The function will return a value after calculations and store it inside the test variable.

Things that functions and sub's have common in them

1. both of them are self contained blocks(procedures)

2. Both of them do some task

 

Have any doubt dont hesitate to ask me in the idealpogrammer forums or via comment.

Here are the videos which are related to this topic –

)

 

 

Thanks!!