Scope of Variables. A variable can have something called a ‘scope’. This refers to its accessibility. Scope of Variable Mr.Skelton …Dalriada School Scope.

Slides:



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

CS0004: Introduction to Programming Repetition – Do Loops.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Functions.
Do Now Noticing skills What does input do?. Annotate your code to explain what happens name = input( “What is your name?\n” ) print(“Hello ”, name) Extension-Python.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Data Types Integer 15 StringHelloThere! Float/Real BooleanYes / No CharP.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Sharda University P. K. Mishra (Asst.Prof) Department of Computer Science & Technology Subject Name: Programming Using C Sub Code: CSE-106 Programming.
Friends = ['Tom', 'Sue', 'Danny', 'Joe', 'Mary'] Index positions >>>Friends[2] Danny >>>Friends[2:4] Danny, Joe >>>Friends[-4] Sue Use your PYTHON.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
CPS120: Introduction to Computer Science Functions.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Python Functions.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
User-Defined Functions II TK1914: C++ Programming.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Object Oriented Programming Criteria: P2 Date: 07/10/15 Name: Thomas Jazwinski.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
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.
Chapter 9: Value-Returning Functions
Introduction to Programming
Worked Examples - Incremental Software Development Worked Example
Function There are two types of Function User Defined Function
Elements of the PHP Programming Environment
Procedures Programming Guides.
Chapter 3 Simple Functions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Teaching London Computing
CS212: Object Oriented Analysis and Design
Chapter 4 void Functions
Functions and Procedures
CS 1111 Introduction to Programming Fall 2018
We’re moving on to more recap from other programming languages
Compound Statements A Quick Overview
3.1 Iteration Loops For … To … Next 18/01/2019.
Namespaces – Local and Global
The lifespan of a variable
Debuggers and Debugging
Documentation and Style
Guess the letter!.
The Function Prototype
Variables In today’s lesson we will look at: what a variable is
Year 10 & 11 Programming Similar, But Different!
PYTHON LESSON 5 Mr. Kalmes.
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
Chapter (3) - Procedures
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.
Methods Scope How are names handled?
Scope Rules.
Presentation transcript:

Scope of Variables

A variable can have something called a ‘scope’. This refers to its accessibility. Scope of Variable Mr.Skelton …Dalriada School Scope of Variable Mr.Gamble …G4 Scope of Variable President Obama …The Whitehouse!

A variable can be declared in one of two ways… When it is accessible from all parts of a program (ie. Anywhere!) A GLOBAL Variable. When it is accessible ONLY within a function/procedure. A LOCAL Variable.

Look at the following PYTHON program: name = ‘Mary Poppins’ print(name) So far, we have been working with variables in this manner. Note that I can call the variable name from ANYWHERE in the program…i.e. it is accessible from anywhere! These declarations have usually been done at the start of our programs, and have been declared OUTSIDE of any functions. These are referred to as GLOBAL variables.

Look at the following PYTHON program: def Greeting(): name = 'Mary Poppins' age = 200 address = 'Far Far away' Notice that these variables have been declared inside a function. This means, the variables are only accessible from within the fucntion. Hence, we have declared LOCAL variables here. If I typed print(name) outside of this function, it would result in a compile error!

So far, we have learnt some important facts about GOOD programming… 1.Use meaningful identifiers for Variable names: e.g. Name and not x 2.Use commenting to aid understanding: e.g. #this function checks to see if a letter is in the phrase Best programming practice avoids use of global variables and in favour of local variables. 3.Avoid use of global variables in favour of local variables.

Result ? 36 NameError: name 'fixed_number' is not defined

Look carefully at this code… This simply asks the user for a number and then calls a function to increment each time the CheckGuess function is called. Type this into Python & see what happens

Look carefully at this code… Note the use of the keyword global. This allows functio to modify/change the value of the global variable guesses! Insert global guesses

Task You are expected to use Functions in your solution