Engineering 1020 Introduction to Programming Peter King Winter 2010.

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

CPSC 388 – Compiler Design and Construction
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
CS 201 Functions Debzani Deb.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Introduction to Programming with Java, for Beginners Scope.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
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:
Functions g g Data Flow g Scope local global part 4 part 4.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Storage Classes, Scope, and Recursion Lecture 6.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 8: Functions, File IO.
CS 1308 Computer Literacy and The Internet Software.
Computer Science: A Structured Programming Approach Using C1 4-6 Scope Scope determines the region of the program in which a defined object is visible.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
Subroutines. Harder Problems  Examples so far: sum, sum between, minimum –straightforward computation –small number of intermediate variables –single.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 6 Methods Chapter 6 - Methods.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Advanced Programming in C
Introduction to Programming
C Functions -Continue…-.
Building Java Programs
About the Presentations
Building Java Programs
Names, Binding, and Scope
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Topics discussed in this section:
Local Variables, Global Variables and Variable Scope
CS2011 Introduction to Programming I Methods (II)
Writing Functions.
Building Java Programs
Quiz: Computational Thinking
Scope Rules.
Presentation transcript:

Engineering 1020 Introduction to Programming Peter King Winter 2010

ENGI 1020: Scope When we create an identifier (variable, function name), there are limitations on where it can be used. Scope is the region of the program where an identifier can be used.

ENGI 1020: Scope Two main types of scope Local identifiers declared within a code block {} scope extends to end of block Global identifiers declared outside of any code block scope extends to end of source file

ENGI 1020: Scope Global scope example //myFunction is declared outside block void myFunction(int a, int b); int main(){ //I can use it here myFunction(1,2); return 0; } //... and here void myFunction(int a, int b){ //do something }

ENGI 1020: Scope Local scope example void myFunction(int a, int b); int main(){ int lVar = 0; //declared in block //I can use it here myFunction(lVar,2); return 0; } void myFunction(int a, int b){ lVar = 5; //Uh oh, can't use it here }

ENGI 1020: Scope Scope allows us to re-use identifiers C++ will assume the most local version of an identifier Visibility is the nature of the compiler focusing on those variables that are in the closest scope See Online examples

ENGI 1020: Scope Try to keep the scope of variables small Prefer local variables over global variables. Use parameters rather than global variables to pass information to functions. If your parameter list is too long maybe your function is doing too much.

ENGI 1020: Design 1. Divide the problem into independent sub-problems that are easier to solve. 2. Start with a high-level solution to the problem, few abstract steps 3. Gradually refine solution into an concrete solution (i.e., algorithm) by adding detail, break high level steps into smaller functional units

ENGI 1020: Design Online example