Sub Procedures and Functions

Slides:



Advertisements
Similar presentations
Manipulating Strings String Functions. VB provides a large number of functions that facilitate working with strings. These are found in Microsoft.VisualBasic.Strings.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Proyek Queri-queri Lanjut dan Sub-subqueri. Variables dan Data.
Modular Programming Splitting your program into functions and procedures.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th.
Chapter 4 - Visual Basic Schneider
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Introduction to Computing Dr. Nadeem A Khan. Lecture 14.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Apply Sub Procedures/Methods and User Defined Functions
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
CS0004: Introduction to Programming Variables – Numbers.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
CS0004: Introduction to Programming Variables – Strings.
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.
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.
Software Development Topic 3 High Level Language Constructs.
1 E0001 Computers in Engineering Built in Functions.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
String and General Procedures. Answer to the last question of Exam 1 Start Get a number Divide the Number By 2 Is the quotient Equal to 1? Print The Remain.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
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.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Data and variables in Visual Basic. Annoucement Lecture on Thursday 7:30PM C106 Visual Basic download: 
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about.
Visual Basic Fundamental Concepts
Visual Basic 6 (VB6) Data Types, And Operators
Value-Returning Functions
Lesson 16 Sub Procedures Lesson 17 Functions
Lesson 18 Math Functions Lesson 19 Format Functions
Functions CIS 40 – Introduction to Programming in Python
Variables and Arithmetic Operations
CIS16 Application Development and Programming using Visual Basic.net
CIS16 Application Development Programming with Visual Basic
VBScript Session 10.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
COMPUTER PROGRAMMING SKILLS
Internal Functions.
Introduction to Computer Programming IT-104
Introducing Modularity
Presentation transcript:

Sub Procedures and Functions Visual Basic Sub Procedures and Functions

Topic & Structure of the lesson Introduction to Modular Design Concepts Write Sub Procedures Compare between Call by Value and Call by Reference

Write a sub program and pass arguments to it Learning Outcomes At the end of this lecture you should be able to : Declare a sub program Write a sub program and pass arguments to it Write a Call-By-Value and Call By Reference sub procedure

Key Terms you must be able to use If you have mastered this topic, you should be able to use the following terms correctly in your assignments and tests: Call By Ref By Val

Modular Design Concepts Modularity refers to breaking a large problem down into smaller self-contained modules. This allows the programmer to isolate problems within the program by looking at specific areas. Without Breaking problems down into smaller modules will increase program maintenance cost.

Dividing code into procedures Divide & Conquer Approach Makes program development more manageable. Software Reusability Using existing procedures as building blocks for new programs Avoid Duplicating programs

Different Modular Techniques Visual Basic provides us with the ability to represent modules through the use of the following:- Sub Procedures Function Procedures Event Procedures

Sub Procedures What are Sub Procedures? Sub Procedures are subprograms or routines which are self contained and are used to perform a particular task.

Writing a Sub Procedure Private Sub cmdAdd_click() Call add(2,1) End sub Private sub add(num1,num2 as integer) Dim total as integer total = num1 + num2 MsgBox total End Sub Arguments parameters

Quick Review Question Write a program to accept two numbers using textboxes and pass the numbers into a subprogram called product to multiply them. Display the answer in the product procedure. Use a Msgbox to display the answer

Answer Private Sub cmdmultiply_Click() Dim a,b as integer Call product (a, b) End Sub Private Sub product (num1 As integer, num2 As integer) Msgbox ("The product of “ & num1 & "and“ & num2 & "is“ & num1 * num2)

No Arguments – No return value MAIN PROGRAM Private Sub Command1_Click( ) ‘ Check the parenthesis. There is no argument Call No_arg_No_return End Sub No arguments

No Arguments – No return value SUB PROCEDURE Public Sub No_arg_No_return() Dim x, y, z As Integer x = 20 y = 30 z = x * y Label1.Caption = "TOTAL = " & z End Sub

OUTPUT

Quick Review Question Write a program that will accept three numbers and pass the numbers to a procedure minimum to determine the smallest number. Accept the three numbers using input box and display the smallest number on a text box.

Solution Private Sub cmdSmallest_Click() Dim value1 As Long, value2 As Long, value3 As Long value1 = txtOne.Text value2 = txtTwo.Text value3 = txtThree.Text Call Minimum(value1, value2, value3) End Sub Private Sub Minimum(min As Long, y As Long, z As Long) If y < min Then min = y End If If z < min Then min = z lblSmallest.Caption = "Smallest value is " & min

Call By Reference When you pass a variable to a sub procedure and return its updated value By default Call By Reference is used in VB

Call By Reference - Procedure Private sub a() dim a as integer a = 5 call square(a) print a end sub private sub square(num as integer) num = num * num Ans a = 25

Call By Value When you pass a variable to a sub program and it does not affect the value from the main program

Call By Value Private sub a() dim a as integer a = 5 call square(a) print a end sub private sub square (byval num as integer) num = num * num Ans a = 5

Group Exercise Write a program that accepts a student mark and passes the mark to a subprogram called grade which will determine the students grade. Mark Grade 75-100 A 60-74 B 50- 59 C 40-49 D 0 – 39 F

Follow Up Assignment Write a program that uses a Sub procedure Maximum to determine the largest of three Integers. The smallest value is displayed in a Label.

String Functions The Len Function The Len function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The syntax is Len (Phrase) For example, Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22 The Len function can also return the number of digits or memory locations of a number that is stored in the computer. For example, X=sqr (16) Y=1234 Z#=10# Then Len(x)=1, Len(y)=4, and Len (z)=8

String Functions The Right Function The Right function extracts a substring from a phrase, starting from the Right. The syntax is Right (Phrase, n) where n indicates the number of characters that you wish to extract starting from the right-most character.  For example,  Right(“Visual Basic”, 4) = asic The Left Function The Left function extracts a substring from a phrase, starting from the left. The syntax is Left(Phrase, n) where n indicates the number of characters that you wish to extract starting from the left-most character..  For example,  Left (“Visual Basic”, 4) = Visu

String Functions The Ltrim Function The Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is Ltrim(Phrase) .For example,  Ltrim (“  Visual Basic”, 4)= Visual basic Rtrim Trim Ucase and the Lcase

String Functions The Mid Function The Mid function extracts a substring from the original phrase or string. The syntax is: Mid(phrase, position, n) Where position is the starting position of the phrase from which the extraction process will start and n is the number of characters to be extracted. For example, Mid(“Visual Basic”, 3, 6) = ual Bas The InStr function The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is Instr (n, original phase, embedded phrase) Where n is the position where the Instr function will begin to look for the embedded phrase. For example Instr(1, “Visual Basic”,” Basic”)=8

String Functions The Rnd Function Rnd is is very useful function for dealing with the concept of chance and probability. The Rnd function returns a random value between 0 and 1. The Numeric Functions The numeric functions are Int, Sqr, Abs, Exp, Fix, Round and Log. a) Int is the function that converts a number into an integer by truncating its decimal part and the resulting integer is the largest integer that is smaller than the number. For example, Int(2.4)=2, Int(4.8)=4, Int(-4.6)= -5, Int(0.032)=0 and so on. b) Sqr is the function that computes the square root of a number. For example, Sqr(4)=2, Sqr(9)=2 and etc. c) Abs is the function that returns the absolute value of a number. So Abs(-8) = 8 and Abs(8)= 8.

Numeric Functions d) Exp of a number x is the value of ex. For example, Exp(1)=e1 = 2.7182818284590 e) Fix and Int are the same if the number is a positive number as both truncate the decimal part of the number and return an integer. However, when the number is negative, it will return the smallest integer that is larger than the number. For example, Fix(-6.34)= -6 while Int(-6.34)=-7. f) Round is the function that rounds up a number to a certain number of decimal places. The Format is Round (n, m) which means to round a number n to m decimal places. For example, Round (7.2567, 2) =7.26 g) Log is the function that returns the natural Logarithm of a number. For example, Log 10= 2.302585