Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Guide to Programming with Python
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Fundamentals of Python: From First Programs Through Data Structures
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
Chapter 6: Functions.
Factoring Expressions
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
DECOMPOSITION. KEY TERMS  Structured programming  Functionality  Structure Charts  Stepwise refinement  Modularity.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Fundamentals of Python: First Programs Chapter 6: Design with Functions.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Recursion Lakshmish Ramaswamy.
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
CS 326 Programming Languages, Concepts and Implementation
Department of Computer Science,
Chapter 6 Functions.
Fundamentals of Programming I Managing the Namespace
Programming Fundamentals Lecture #7 Functions
#include "std_lib_facilities
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Topics Introduction to File Input and Output
CS 1111 Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Fall 2018
5. Functions Rocky K. C. Chang 30 October 2018
Namespaces – Local and Global
Namespaces How Shall I Name Thee?.
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Chapter 20: Programming Functions
1-6 Midterm Review.
Objectives In this chapter, you will:
Introduction to Computer Science
Topics Introduction to File Input and Output
Namespaces – Local and Global
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1

Namespaces Namespace: – List of all variables and their current values – Maintained by Python environment Example: x8 y15 z“hello”

Namespaces and Functions Each function has different namespace Variable in function has no effect on same variable in any other function – Like two “separate universes” Spock in main program Spock in separate function

Namespace Example Namespace for main program x 2 Namespace for scope_demo x 1

Arguments and Namespaces Arguments to functions are also local to a namespace – Example: Fahrenheit to Celsius program – Note that f and c are different in function, main program Namespace for fahrenheit2celsius f c Namespace for main program f c

Arguments and Namespaces Key idea: Does not matter whether same or different variable names used in functions – Will be treated as different variables regardless Example: Two versions of fahreheit2celsius program – Both do the same thing

Passing Arguments by Value Key idea: – Variables not passed or returned – Only their values are passed/returned – In both cases the value of 50 passed and the value of 10 returned Namespace for fahrenheit2celsius f 50 c 10 Namespace for main program f 50 c 10 Namespace for fahrenheit2celsius f 50 c 10 Namespace for main program x 50 y 10

Namespaces and Functions Why is this important? – Same function may be called from multiple places with different arguments Example: “Largest of 4 numbers” program – Programs often created by hundreds of programmers Each creates set of functions Should not have to worry about what variables other programmers are using! – Functions often put in separate files (modules) No main program associated with it!

Functions Calling Functions Separate namespace created for each call Namespace for main program x 2 Namespace for scope_demo x 1 Namespace for scope_demo2 x 7

Activation Stack Each current namespace = “activation record” Maintained on “stack” of activation records – When function called, its namespace pushed on top of stack – When return from function, that namespace removed – Return to function immediately below it on stack main program x 2 scope_demo x 1 scope_demo2 x 7 main program x 2 x 2 scope_demo x 1 main program x 2 scope_demo x 1 main program x 2

Stack for Population Program Main program get_ years Main program print_ table Main program print_ table compute _populat ion Main program print_ table Main program print_ table print_ line Main program print_ table Main program print_ table compute _populat ion Main program print_ table Main program print_ table print_ line

Scoping Key idea: Namespace only exists while function active Scope of variable = function it is defined in – Does not exist outside of its scope (error if referenced)

Scoping and Global Variables Exception: Functions may refer to variables in main Exception to the exception: If value assigned in function it becomes local to function Just avoid doing this! Prints 3 Still 3