COM148X1 Interactive Programming Lecture 4. Topics Today DateTime Functions Using Module Using Procedure Using Function Pass by Value and Pass by Reference.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Sub and Function Procedures
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
1.
Chapter 4 General Procedures
Chapter 4 - Visual Basic Schneider
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Variables and Constants
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
Why to Create a Procedure
COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Microsoft Access Using Visual Basic Routines. Visual Basic Datatypes Boolean Byte Currency Date Double Integer Long Object Single String Variant Hyperlink.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
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.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 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.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Types of Visual Basic Data Numeric Data Non Numeric Data.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
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.
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.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
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.
Lecture 7 Methods (functions and subroutines) Parameter Passing
CS0004: Introduction to Programming
Introduction to Programming Lecture 3 Msury Mahunnah, Department of Informatics, Tallinn University of Technology.
Sub Procedures And Functions
Use TryParse to Validate User Input
Visual Basic I Programming
Visual Basic Fundamental Concepts
CIS199 Test Review 2 REACH.
COMPUTATIONAL CONSTRUCTS
Royal University of Phnom Penh
Control Structures II Chapter 3
Lesson 16 Sub Procedures Lesson 17 Functions
Functions Chapter 6-Part 2.
Use TryParse to Validate User Input
Method.
2. Understanding VB Variables
Chapter 4 - Visual Basic Schneider
Visual Basic..
Procedures and Functions
Introduction to Object-Oriented Programming (OOP)
Chapter 7: Using Functions, Subs, and Modules
Libraries of Code Notes from Wilson, Software Design and Development Preliminary Course pp
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Dictionary Builder Part 4 Loading Files.
STARTING OUT WITH Visual Basic 2008
COMPUTER 2430 Object Oriented Programming and Data Structures I
10.3 Procedures Function Procedures 07/06/2019.
Presentation transcript:

COM148X1 Interactive Programming Lecture 4

Topics Today DateTime Functions Using Module Using Procedure Using Function Pass by Value and Pass by Reference

DateTime Functions

DateTime Class Visual Basic provides many useful date and time functions and they can be used through all DateTime object All DateTime functions will not alter the original DateTime, another DateTime variable is required to hold the result

Commonly Use DateTime Functions FunctionDescriptionExample Year() get the year (integer) of a date x = d.Year() Month() get the month (integer of a date x = d.Month() Day() get the day (integer) of a date x = d.Day() Hour() get the hour (integer) of a date x = d.Hour() Second() get the second (integer) of a date x = d.Second() DayOfWeek() get the day of week (integer, 0 – Sunday, 6 – Saturday) x = d.DayOfWeek() DayOfYear() get the day of year (1 – 366) x = d.DayOfYear()

Commonly Use DateTime Functions (cont’) FunctionDescriptionExample Now() get the current date and time d2 = d.now() AddYear(y) add y years to date d2 = d.AddYear(3) AddMonth(m) add m months to date d2 = d.AddMonth(2) AddDay(d) add d days to date d2 = d.AddDay(10) AddHour(h) add h hours to date d2 = d.AddHour(12) AddSecond(s) add s seconds to date d2 = d.AddSecond(50) IsLeapYear() Check if the year of date is leap year if d.IsLeapYear() then …

DateTime Format DateTime can be converted from/ to String in the following format d08/17/2000 DThursday, August 17, 2000 fThursday, August 17, :32 PM FThursday, August 17, :32:00 PM g08/17/2000 4:32 PM G08/17/2000 4:32:00 PM mAugust 17 rThu, 17 Aug :32:00 GMT s T16:32:00 t4:32 PM T4:32:00 PM u :32:00Z yAugust, 2000

DateTime Example Dim d as DateTime ‘ convert String to DateTime d = “Thursday, August 17, :32 PM” Dim s as String ‘ convert DateTime to String s = d.ToString(“g”) ‘ s = “08/17/2000 4:32 PM ”

Using Module

What is Module Module is group of functions, procedures and variables using throughout all the form Module can be add to other project and the functions, procedures and variables can be reused

Create Module Step 1

Create Module Step 2

Create Module Step 3

Define Module Module module_name End Module The code will be generated by Visual Basic if previous steps are followed

Declare Variable in Module Public variable_name As data_type Example Public MaxStudent as Integer = 20 Variables declared in module can be accessed by other modules/ forms in the same project

Using Procedure

What is Procedure Procedure is a segment of code which responsible for a sub-task of the main task Advantages of using procedure Make the program easy to design and implement Make the code easy to reuse Make the code easy to read Make the code easy to modify

One Bulky Procedure Enter list of valid item code, calculate the total cost of stock and format the result into a report

Break Down of Procedure Enter item code Check for valid code Add cost of stock More item? y Format and print report n Valid? n y

Define Procedure Sub procedure_name( ByVal pname1 As ptype1, … ) procedure End Sub Example Sub AddStock(ByVal stock_count As Integer) total_cost = total_cost + stock_count * 5 End Sub

Using Function

What is Function Function is similar to procedure but it will return a value back to the caller

Function A1 A2 A3 F v1 v2 v3

Define Function Function function_name( ByVal pname As ptype, … ) As output_type function calculation function_name = value End Function Example Function CheckCode(ByVal code As Integer) As Boolean If code 100 Then CheckCode = False Else CheckCode = True End If End Function

Pass by Value and Pass by Reference

Pass by Value If parameters set as ByVal, then it is “Pass by Value”, all modifications to this parameter during the execution of function/ procedure will not retain (i.e. the value of the parameter will be restored back to the value before the function/ procedure execute)

Pass by Value Example Function CheckCode(ByVal code As Integer) as Boolean code = code mod 100 ‘get leftmost 2 digits if (code mod 7 = 0) then CheckCode = True Else CheckCode = False End If End Sub Sub Proc() Dim c As Integer Do c = InputBox(“Input Code”) If ( Not CheckCode(c) )Then MsgBox(“Invalid Code ” & c) ‘no change in value of c End if Loop While ( Not CheckCode(c) ) End Sub

Pass by Reference If parameters set as ByRef, then it is “Pass by Reference”, all modifications to this parameter during the execution of function/ procedure will retain

Pass by Reference Example Sub Swap(ByRef x As Integer, ByRef y As Integer) Dim temp as Integer temp = x x = y y = temp End Sub Sub Proc() Dim x As Integer = 1 Dim y As Integer = 2 Swap(x, y) ‘value of x and y will be swapped End Sub