Understanding Subroutines, Functions, and Parameters in Programming

introduction to programming n.w
1 / 18
Embed
Share

Learn about the differences between subroutines and functions in programming, the use of parameters, ByVal and ByRef distinctions, as well as how to define and call these procedures. Explore examples and their general forms to enhance your programming knowledge.

  • Subroutines
  • Functions
  • Parameters
  • Programming
  • Procedures

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Introduction to Programming Lecture 3 Msury Mahunnah, Department of Informatics, Tallinn University of Technology

  2. Two type of procedures Subroutines (Sub ... End Sub) perform actions and they don t return any result Functions (Function ... End Function) perform some calculations and return a value A simple and short program does not need them. It is enough to use automatically appearing object s event procedures (subroutines), For instance: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End Sub

  3. Subroutine general form Sub subroutine_name({param As varType}) Statement(s) {Exit Sub} End Sub {} this part of the structure may miss or appear discretionary times varType discretionary type (Integer, Long, Single, Double, Char, Date, ...) param = parameter - a special kind of variable, that is used for passing information between procedures There are two kinds of parameters - input (ByVal) and output (ByRef) parameters

  4. ByVal and ByRef ByVal is short form for ByValue ... it means that you are passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered. ByRef is short form for By Reference ... this means that you are not handing over a copy of the original variable but pointing to the original variable Declaring a parameter with ByVal, we call it input parameter Declaring a parameter with ByRef, we call it output parameter

  5. Function general form Function function_name({param As varType}) As varType Statement(s) {Exit Function} {Return return_value} {function_name = return_value} End Function

  6. Subroutine in use Sub ShowDate() MsgBox(Format(Now(), "dd.mm.yyyy")) End Sub . Format style To call (execute) this subroutine: ShowDate() or Call ShowDate() Now() built-in function that returns Date value containing the current date and time according to your system Format built-in function that returns a string formatted according to instructions contained in a format String expression

  7. Subroutine in use 2 Using a parameter Sub ShowDate(ByVal myDate As Date) MsgBox(Format(myDate(), "dd.mm.yyyy")) End Sub Format style To call (execute) this subroutine: ShowDate(#3/21/2011#) or Call ShowDate(#3/21/2011#) myDate parameter Format built-in function that returns a string formatted according to instructions contained in a format String expression.

  8. Function in use ByVal define that n is input parameter Function Fact(ByVal n As Int) As Int Dim i As Int, F As Int F = 1 For i = 1 To n F = F * i Next Return F End Function Function Fact(ByVal n As Int) As Int Dim i As Int, F As Int F = 1 For i = 1 To n F = F * i Next Fact = F End Function difference To call (execute) this function: Dim factorial As Int = Fact(5)

  9. Function in use 2 Function NextDay() As Date Dim theNextDay As Date theNextDay = Now.AddDays(1) Return theNextDay End Function To call (execute) this function: Dim tomorrow As Date = NextDay()

  10. Repetition without Loop-statement Repetition with a recursion - a function calls (executes) itself. Function FactRec&(ByVal n&) If n = 1 ThenReturn 1 FactRec = n * FactRec(n - 1) End Function

  11. Converting Variable Types The methods of the Convert class that perform data-type conversions: ToBoolean, ToByte, ToChar, ToDateTime, ToDecimal, ToDouble, ToInt16, ToInt32, ToInt64, ToSByte, ToShort, ToSingle, ToString, ToUInt16, ToUInt32, ToUInt64 Example: Dim x$ = 21.03.2011", y As Date y = Convert.ToDateTime(x) y = #3/21/2011#

  12. Converting Variable Types 2 Functions that perform data-type conversions: CBool, CByte, CChar, CDate, CDec, CDbl, CInt, CLng, CSByte, CShort, CSng, CStr, CUInt, CULng, CUShort Example: Dim x$ = 21.03.2011", y As Date y = CDate(x) y = #3/21/2011#

  13. Functions for checking data type IsNumeric() Returns True if its argument is a number (Short, Integer, Long, Single, Double, Decimal). IsDate() Returns True if its argument is a valid date (or time). IsArray() Returns True if its argument is an array.

  14. An example with IsNumeric (TextBox) Private Sub Button1_Click(ByVal sender As System.Object, ...) Dim x% If Not IsNumeric(TBox_Age.Text) Then MsgBox("A number is expected!") TBox_Age.Text = "" TBox_Age.Focus() Exit Sub End If x = Convert.ToInt32(TBox_Age.Text) floating-point number??? MsgBox("You are " & x & " years old!") End Sub Three objects on the form are needed: TextBox with a Name TBox_Age Button with a Name Button_1 Label with a Text Age

  15. A example with IsNumeric (InputBox) Private Sub Button1_Click(ByVal sender As System.Object, ...) Dim strAge As String = "" Dim Age As Integer Do While Not IsNumeric(strAge) strAge = InputBox( Please enter your age") Loop Age = Convert.ToInt32(strAge) if strAge is a floating-point number? MsgBox("You are " & Age & " years old!") End Sub A object on the form is needed: Button with a Name Button_1

  16. Try ... Catch Try ... Catch statement At first, execute sentence(s)1, but if the error appear, execute sentence(s)2 Try sentence(s)1 Catch [ex As Exception] sentence(s)2 [Finally sentence(s)3] End Try [ ] this part of structure is not necessary

  17. Try ... Catch and Textbox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Age% Try Age = Convert.ToInt32(TBox_Age.Text) MsgBox("You are " & Age & " years old!") Catch ex As Exception MsgBox(ex.Message) 'MsgBox("A number is expected!") Finally TBox_Age.Text = "" TBox_Age.Focus() End Try if an error appears go there End Sub

  18. Try ... Catch and Inputbox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strAge As String = "" Dim Age As Integer Do strAge = InputBox("Please enter your age") Try Age = Convert.ToInt32(strAge) if error here, go to there Exit Do Exit Do when previous sentence doesn t give error Catch ex As Exception MsgBox(ex.Message) End Try Loop MsgBox("You are " & Age & " years old!") End Sub

More Related Content