More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
VBA Modules, Functions, Variables, and Constants
CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th.
Example 2.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
Chapter 41 Sub Procedures, Part II (Continue). Chapter 42 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Ch 10: More on Variables and Subroutines CP212 Winter 2012.
IE 212: Computational Methods for Industrial Engineering
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Why to Create a Procedure
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
A Level Computing#BristolMet Session ObjectivesU2#S10 MUST describe the difference between constants, local and global variables SHOULD explain why constants.
Lecture 8 Visual Basic (2).
Functions, Pointers, Structures Keerthi Nelaturu.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Visual Basic Programming I 56:150 Information System Design.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Scope Lifetime Modules Procedures. Scope? Where can your variables be seen? Where used? Where abused (reseting the value)? Local and Global = Private.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
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,
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Spreadsheet-Based Decision Support Systems
JavaScript: Functions
Function There are two types of Function User Defined Function
Student Book An Introduction
Variables and Their scope
Object Oriented Programming in java
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Chapter 9: Value-Returning Functions
Predefined Functions Revisited
Scope Rules.
C Parameter Passing.
Presentation transcript:

More on Variables and Subroutines

Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module or macro may have many subs or functions. Variables are not always created in a particular sub or function but may be used by these procedures. Where a variable is created will be the scope of that variable.

Scope of Variables and Subroutines A variable created with a Dim statement inside a sub is called a procedure-level variable. A variable created a the top of a module with a Dim statement is a module-level or global variable. –Every sub in the module has access to this variable. Instead if Dim, you can use Private which also has a module-level scope. Another option is to use Public which is a project-level or global scope. This is often used with two or more modules in a project. Variables declared module or project level scope should not be declared in a sub with a Dim statement. Subroutines by default have a Public scope. When you define a Public sub then any other sub in the entire project can call this sub. Subroutines created using Private are only callable by subs within its module.

Passing Control of Subs The main sub is used to call or invoke the other subs or functions. Subs are called or invoked using the Call statement. The arguments used by the sub are listed in parenthesis after the sub procedure’s name. The use of sub and function procedures is an attempt to modularize a project. Allows you to break up code in smaller parts.

Passing Arguments There are two ways sub procedures can access external variables: –1. by using module-level variables –2. by passing arguments Example 1: Const PI As Double = 22/7 Sub Main() Dim R as Integer, A as Double R = Inputbox(“Enter the radius of a circle”,”Input”) A = Area(R) Range(“A1”).Value = “The Area of the Circle is “ & A End Sub Private Function Area(R as Integer) As Double Area = PI * R^2 End Sub Example 2: Sub Main() Dim R as Integer, A as Double R = Inputbox(“Enter the radius of a circle”,”Input”) A = Area(R) Range(“A1”).Value = “The Area of the Circle is “ & A End Sub Private Function Area(R as Integer) As Double Const PI As Double = 22/7 Area = PI * R^2 End Function

Passing Arguments By Reference and By Value Arguments passed by Reference is the default method and passes a reference to the variable being passed. When variables are passed by Reference, the original value of the variable can be changed permanently. When variables are passed by Value only a copy of the original variable’s contents are available to the calling procedure. Arrays are passed to procedures (Sub or Function) by Reference since the elements are generally manipulated. The physical address in memory cannot be changed and passed by Value. Function subroutines always return either a value, Boolean, or string result. A = Area(R) is an example of invoking a Function. The variable A will also receive the result passed from Area.

Concatenating Names And Random Numbers You can concatenate names two ways: –1. You can use the + sign, “Joe ” + “College” –2. You can use the & symbol, “Joe “ & “College” –The second rendition is the preferred style. Random numbers are generated using the function Rnd. You can use Int function to retrieve the integer portion generated. The actual function generates numbers between 0 and 1. The function Randomize is a seed function that assures the numbers generated each time are random. Example: number = 1 + Int (Rnd * 100) will generate numbers between 1 and 100.

Passing Arguments There are two ways sub procedures can access external variables: –1. by using module-level variables –2. by passing arguments Example 1: Const PI As Double = 22/7 Sub Main() Dim R as Integer, A as Double R = Inputbox(“Enter the radius of a circle”,”Input”) A = Area(R) Range(“A1”).Value = “The Area of the Circle is “ & A End Sub Private Function Area(R as Integer) As Double Area = PI * R^2 End Sub Example 2: Sub Main() Dim R as Integer, A as Double R = Inputbox(“Enter the radius of a circle”,”Input”) A = Area(R) Range(“A1”).Value = “The Area of the Circle is “ & A End Sub Private Function Area(R as Integer) As Double Const PI As Double = 22/7 Area = PI * R^2 End Function

Passing Arguments By Reference and By Value Arguments passed by Reference is the default method and passes a reference to the variable being passed. When variables are passed by Reference, the original value of the variable can be changed permanently. When variables are passed by Value only a copy of the original variable’s contents are available to the calling procedure. Arrays are passed to procedures (Sub or Function) by Reference since the elements are generally manipulated. The physical address in memory cannot be changed and passed by Value. Function subroutines always return either a value, Boolean, or string result. A = Area(R) is an example of invoking a Function. The variable A will also receive the result passed from Area.

Concatenating Names And Random Numbers You can concatenate names two ways: –1. You can use the + sign, “Joe ” + “College” –2. You can use the & symbol, “Joe “ & “College” –The second rendition is the preferred style. Random numbers are generated using the function Rnd. You can use Int function to retrieve the integer portion generated. The actual function generates numbers between 0 and 1. The function Randomize is a seed function that assures the numbers generated each time are random. Example: number = 1 + Int (Rnd * 100) will generate numbers between 1 and 100.