School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,443 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,538 people online right now. Registration is fast and FREE... Join Now!




Functions & Subroutines

 
Reply to this topicStart new topic

> Functions & Subroutines, What are they? What they do?

AdamSpeight2008
Group Icon



post 6 Apr, 2009 - 03:02 PM
Post #1


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

CODE

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.

CODE

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

CODE

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


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

CODE

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.
CODE

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.
CODE

Sub ClearTextbox(ByRef Tb As Textbox)
Tb.Text=""
End Sub



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

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

This post has been edited by AdamSpeight2008: 7 Apr, 2009 - 06:04 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

AdamSpeight2008
Group Icon



post 7 Apr, 2009 - 11:03 AM
Post #2
Postscript

A Subroutine or Function that calls itself is called recursive. This process is call recursion, and you have to be careful.
CODE

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
CODE

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




Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 01:31AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month