Nate Brunelle Today: Functions again, Scope

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Scope and Lifespan of Identifiers. Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS-212 Classes (cont) Dick Steflik. Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state.
Functions Dilshad M. Shahid New York
Slide 1. Slide 2 Administrivia Nate's office hours are Wed, 2-4, in 329 Soda! TA Clint will be handing out a paper survey in class sometime this week.
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
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:
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
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.
Ferris wheel. The London Eye It was built in 1999.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1110 Introduction to Programming Spring 2017
Chapter 10: Void Functions
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
A Lecture for the c++ Course
Implementing Functions from a Detailed Design Quick Tips
This pointer, Dynamic memory allocation, Constructors and Destructor
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Teaching London Computing
Basic C++ What’s a declaration? What’s a definition?
User Defined Functions
Nate Brunelle Today: PyCharm
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Dynamic Memory Allocation
Passing Parameters by value
Chapter 8 Namespaces and Memory Realities
Functions In Matlab.
Lesson 06: Functions Class Chat: Attendance: Participation
Nate Brunelle Today: Functions again, Scope
Writing Functions.
Herbert G. Mayer, PSU CS Status 8/2/2013
Nate Brunelle Today: Turtles
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Nate Brunelle Today: Values and Types
Nate Brunelle Today: Conditional Decision Statements
Nate Brunelle Today: Lists
Function “Inputs and Outputs”
Nate Brunelle Today: Functions
Chapter 20: Programming Functions
Nate Brunelle Today: Strings, Type Casting
Nate Brunelle Today: Values and Types
Nate Brunelle Today: Conditional Decision Statements
Relations and Functions.
Is this respect. What does this tell me about you
Department of Computing Science
Introduction to Python
you get to solve puzzles!
Writing Focus: Personal Narratives
Nate Brunelle Today: Conditional Decision Statements
Nate Brunelle Today: Functions
Nate Brunelle Today: Strings, Type Casting
References Revisted (Ch 5)
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Functions, Procedures, and Abstraction
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

Nate Brunelle Today: Functions again, Scope CS1110 Nate Brunelle Today: Functions again, Scope

Questions?

Last Time Functions

Advice on functions Define function in one file, use in another Good names Usually an action phrase You usually don’t want functions with side effects Don’t use print, instead return a str Don’t use input, instead use parameters Use docstrings Tell you why a function exists How to use that function

Visualizing Programs http://www.pythontutor.com/visualize.html

Scope Scope- The area where a variable has meaning Usually: variable is in scope when it is created onward. Functions: variable is in scope only in the function in which it is defined

What happens when you invoke a function? Create memory for the function Copy argument values into parameter names Do what the function says Know what to return, then return it Remove the memory for the function

Which variable v is in scope? If the variable v is available in the local scope, use that one If the variable v is not available in the local scope, use the variable v available in the global scope If a function ever will have the local variable v, you can only use that variable v

Global variables Allow you to have something “remembered” from one run of a function to another.