Page 1 of 1

## Functions & Subroutines What are they? What they do? Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=97671&amp;s=4a678ddd7c81917f37c0e8fe6942cbfb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

• MrCupOfT

Reputation: 2292
• Posts: 9,531
• Joined: 29-May 08

Posted 06 April 2009 - 04:02 PM

Subroutines & Functions

Subroutine

A subroutine is a code construct.
A code construct is a pattern of coding. Examples;- For Loops, Do Whiles Loops, Sub ... End Sub, Function .... End Function
A subroutine is a self contained section of code, that;-
Has Inputs: Possible
Has Output: Never

```Private Sub PrintHelloWorld()
Console.WriteLine("Hello World")
End Sub

```

Where you put your code to handle a button click is particular type of subroutine called An Event Handler
Indicated by the Handles Button1.Click.

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

End Sub

```

Functions
A function is a self contained section of code, that;-
Has Inputs: Generally Yes
Has Output: Always

```Function SquareOfNumber(ByVal x As Decimal) As Decimal
Return x * x
End Function

```

Subroutines & Functions may contain calls to other Functions and Subroutines.

``` Function Factorial(ByVal x As Decimal) As Decimal
Dim f As Decimal = 1
While x > 1
f = f * x
x -= 1
End While
Return f
End Function

Function FactorialSquared(ByVal x Decimal) As Decimal
Return SquareOfNumber(Factorial(x))
End Function

```

Thats the basics of Functions & Subroutines.

Q. What all the arguments of the preceding Functions & Subroutines have in common?
A. All the arguments are preceded by a ByVal.

There are to basic types (the object type of the arguments excluded) of argument.

ByVal This a copy of the object passed in. You can do what you like to this and it won't change the original object passed in as the argument.
ByRef This a REFerence to the actual object. What you do to this affects the original object passed in as the argument.

A ByRef argument allows the programmer to get information out of a Subroutine, like a Function.

Example 1 (Trivial): Increase the value of two Doubles by 2.
```Public Sub IncreaseBothBy2(ByRef A As Double,ByRef B As Double)
A+=2
B+=2
End Sub

```

Example 2: Clear the text of any textbox.
```Sub ClearTextbox(ByRef Tb As Textbox)
Tb.Text=""
End Sub

```

I hope now you'll be a Function & Subroutine wielding Code-Ninja.

Edit: Mismatched tag
Edit: The 100th VB.net Tutorial on DiC

This post has been edited by AdamSpeight2008: 07 April 2009 - 07:04 AM

Is This A Good Question/Topic? 2

## Replies To: Functions & Subroutines

• MrCupOfT

Reputation: 2292
• Posts: 9,531
• Joined: 29-May 08

Posted 07 April 2009 - 12:03 PM

Postscript

A Subroutine or Function that calls itself is called recursive. This process is call recursion, and you have to be careful.
```Function Factorial(ByVal x As UInteger) As UInteger
Return x * Factorial(x-1)
End Function

```

Problem: The above function will continue to call itself, until it filled the stack and the crashes.
```Function Factorial(ByVal x As UInteger) As UInteger
' A base condition to end the recursion.
If x=0 Then Return 1
Return x * Factorial(x-1)
End Function

```

### #3 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 126
• Posts: 555
• Joined: 30-October 10

Posted 13 November 2010 - 06:12 PM

Excellent tutorial!