More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
CPS120: Introduction to Computer Science Functions.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
ISBN Chapter 10 Implementing Subprograms.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Top-down approach / Stepwise Refinement & Procedures & Functions.
Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 9: Value-Returning Functions
Chapter 10 : Implementing Subprograms
Chapter 5: Enhancing Classes
Exam #1 You will have exactly 30 Mins to complete the exam.
Functions and Procedures
Week 8 - Friday CS 121.
Functions CIS 40 – Introduction to Programming in Python
Lecture 11 C Parameters Richard Gesick.
Review for Test1.
Chapter 4 Functions Objectives
Functions, Procedures, and Abstraction
Teach A-Level Computer Science: Object-Oriented Programming in Python
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Engineering Computation with MATLAB
Chapter 8 Namespaces and Memory Realities
CS 1111 Introduction to Programming Fall 2018
Structured Programming Taken from notes by Dr. Neil Moore
Consolidation and Review
Decision Structures and Indefinite Loops
Introduction to Dictionaries
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Homework #5 — Monte Carlo Simulation
Elements of a Python Program
Variables, Lists, and Objects
Debuggers and Debugging
Simple Graphics Package
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
More elements of Python programs
Notes on pyplot Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Note on Program Design Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Notes on Homework #6 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Strings, Lists, and Files
Predefined Functions Revisited
Introduction to Computer Science
Numpy, pylab, matplotlib (follow-up)
Functions, Procedures, and Abstraction
Scope Rules.
Introduction to Classes and Objects
Presentation transcript:

More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 More About Functions

Functions Reading assignment — Chapter 6 What is a function? §6.1 – §6.3 are easy & mostly review §6.4 – §6.6:– read carefully What is a function? A sequence of one or more lines of Python code that can be invoked as a group under a single name (from Week 1) A subprogram to accomplish a specific, defined “sub- computation” (Usually) takes parameters (Usually) returns a result What is a method? A function that lives inside of an object Has access to the “innards” of that specific object CS-2301, D-Term 2014 Introduction

Why do we … … have functions? … have parameters? … have results? To avoid writing the same code in multiple places To encapsulate a sub-idea of the program or problem To solve a subset of a problem and put it out of our minds while we concentrate on the rest of the job! To make it easier to build up a working program … … to solve interesting problems … … … that are too difficult to solve “by hand” … have parameters? To be able to supply values and objects to function So we can apply function to broader range of things … have results? To pass answers back from functions! CS-2301, D-Term 2014 Introduction

Scope Definition:– the region where an identifier is meaningful I.e., where it can be used to refer to the same value, object, function, etc. Scope rule #1:– A variable name (or any other identifier) defined inside a function cannot be seen outside of a function I.e., it is local to that function See bottom p. 175 Variable name ceases to exist when function returns! Comes back into existence as a brand new variable on next invocation This rule is common to (essentially) all modern programming languages Reminder:– Definition of variable Name used to refer to a value, object, list, function, etc. CS-2301, D-Term 2014 Introduction

Why do we have Scope Rule #1? If two people working on different functions in the same project, … … don’t want the local identifiers of one to interfere with the other Even one person working on a large project needs to be able to put details of a function out of mind while working on other functions Include names of all of its local variables CS-2301, D-Term 2014 Introduction

Scope (continued) Scope Rule #2:– A function can see all of the names (i.e., variables, objects, functions, etc.) defined outside that function but in same module Even if defined later in the module But not inside another function! Why Scope Rule #2? So functions can store values (and objects) that persist from one call to next CS-2301, D-Term 2014 Introduction

Importing and Scope What does import do? Answer:– import scopeExample Makes names of one module available to another I.e., adds to the “scope” of the module import scopeExample Makes identifier “scopeExample” available to current scope Implicitly makes all components of scopeExample also available scopeExample.m scopeExample.appendSin() scopeExample.printM() import scopeExample as SE Same as import ScopeExample but gives it a shorthand name ‘SE’ CS-2301, D-Term 2014 Introduction

Importing and Scope (continued) from scopeExample import appendSin from scopeExample import printM Imports identifiers appendSin(), printM() Adds to current scope … but NOT scopeExample CS-2301, D-Term 2014 Introduction

Good/bad Python practice The Python experts advise AGAINST USING from moduleName import * Why? Because it will bring a large number of unknown variables/names into your scope If you (unwittingly) re-use any name … … could cause truly mysterious havoc to your program But we all do it all the time:– from graphics import * It is a good idea to avoid it as much as possible! CS-2301, D-Term 2014 Introduction

Important convention In Python, it is best practice to use ‘_’ at start of function or variable to name to mean This name should be treated as private! Don’t use it! Don’t mess with it! Many other programming languages have means to enforce privacy Python does not! It is just a matter of good practice CS-2301, D-Term 2014 Introduction

Questions? CS-2301, D-Term 2014 Introduction