Page 1 of 1

Functions & Subroutines What are they? What they do? Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Post icon  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.


For the more the adventurous

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. :ph34r:

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

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


Is This A Good Question/Topic? 2
  • +

Replies To: Functions & Subroutines

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • 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.
Solution: Add base conditions
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


Was This Post Helpful? 0
  • +
  • -

#3 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

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

Posted 13 November 2010 - 06:12 PM

Excellent tutorial! :smartass:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1