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

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Midterm 26 March 2015 (4:30-5:30 pm) – Rm5620 Closed book exam MC Questions only x25 Up to L(7) Methods Scope: video lectures (+Lab), forum discussions,
Practical Programming COMP153-08S Lecture: Repetition Continued.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
Data Types 1.
Program 04 (Fall 2014) VB Auto Center Problem Purpose: You are to add Arguments and Parameters to Program 03. See slides 3, 4, and 5 for additional details.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
CS0004: Introduction to Programming Variables – Numbers.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 3 Input, Variables, Constants, And Calculations.
3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Lecture 8 Visual Basic (2).
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
VB .NET Revisit VB .NET Revisit.
Applications Development
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
HNDIT Rapid Application Development
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Sub Procedures And Functions
Use TryParse to Validate User Input
Visual Basic Fundamental Concepts
5.03 Apply operators and Boolean expressions
Subprograms Functions Procedures.
A variable is a name for a value stored in memory.
Programming in visual basic .net Visual Basic Building Blocks
Objective 7.03 Apply Built-in Math Class Functions
Royal University of Phnom Penh
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Labs for week 2.
Data Types, Arithmetic Operations
Single Dimensional Arrays
Use TryParse to Validate User Input
Programmazione I a.a. 2017/2018.
Introduction to VB programming
Final Exam Review Part 4 - VBA
للمزيد زورونا على موقعنا الإلكتروني:
Visual Basic..
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Lecture Set 6 The String and DateTime Data Types
Procedures and Functions
CIS16 Application Development Programming with Visual Basic
Part B – Structured Exception Handling
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
VB.Net Introduction.
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

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

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

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

ByVal and ByRef ByVal is short form for “By Value”... 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

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

Subroutine in use Format style Sub ShowDate() MsgBox(Format(Now(), "dd.mm.yyyy")) End Sub 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.

Subroutine in use 2 Format style Using a parameter Sub ShowDate(ByVal myDate As Date) MsgBox(Format(myDate(), "dd.mm.yyyy")) End Sub 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.

Function in use 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 To call (execute) this function: Dim factorial As Int = Fact(5) 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 Fact = F End Function difference

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()

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

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$ = “ ", y As Date y = Convert.ToDateTime(x) ‘y = #3/21/2011#

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$ = “ ", y As Date y = CDate(x) ‘y = #3/21/2011#

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.

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”

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”

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

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 End Sub if an error appears go there

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