Execution order. main()

Similar documents
Intro. Scheme Basics. scm> 5 5. scm>

Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

Functional Programming. Pure Functional Languages

Introduction to Python (All the Basic Stuff)

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Array. Prepared By - Rifat Shahriyar

CS 314 Principles of Programming Languages

Introduction to Python! Lecture 2

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Functional Programming. Pure Functional Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

COP4020 Programming Assignment 1 - Spring 2011

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS1 Lecture 4 Jan. 23, 2019

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

ENGR 101 Engineering Design Workshop

4.2 Function definitions the basics

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

HIGHER ORDER FUNCTIONS 2

SCHEME AND CALCULATOR 5b

SAMS Programming - Section C. Lecture 1: Introduction + Basic Building Blocks of Programming

Anonymous Functions CMSC 330: Organization of Programming Languages

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

JavaScript: Coercion, Functions, Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

CS1 Lecture 5 Jan. 25, 2019

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Problem Solving for Intro to Computer Science

CMSC 201 Computer Science I for Majors

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Text Input and Conditionals

Higher Order Functions

functional programming in Python, part 2

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 5 Jan. 26, 2018

Data Structures. Lists, Tuples, Sets, Dictionaries

Discussion 11. Streams

CS1 Lecture 4 Jan. 24, 2018

CS1 Lecture 3 Jan. 22, 2018

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Functional programming in LISP

Test #2 October 8, 2015

(Refer Slide Time 01:41 min)

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Warm-up and Memoization

Our Strategy for Learning Fortran 90

CSE : Python Programming

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Visualize ComplexCities

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Python: common syntax

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CS 315 Programming Languages Subprograms

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Guido van Rossum 9th LASER summer school, Sept. 2012

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Lists, loops and decisions

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Python Programming: Lecture 3 Functions

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Mastering Python Decorators

Programming Paradigms in Python

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CSCE 120: Learning To Code

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

TUPLES AND RECURSIVE LISTS 5

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Structure and Interpretation of Computer Programs

6.001 Notes: Section 8.1

Organization of Programming Languages CS3200/5200N. Lecture 11

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

6.001 Notes: Section 6.1

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

61A Lecture 2. Wednesday, September 4, 2013

Repetition CSC 121 Fall 2014 Howard Rosenthal

Racket Style Guide Fall 2017

Functions. Nate Foster Spring 2018

Introduction to Python

T H E I N T E R A C T I V E S H E L L

COMP-202 Unit 4: Programming with Iterations

Transcription:

Functions

Execution order When you load and run a Python module (file), the statements and definitions in the file are executed in the order in which they occur Executing a def defines a function, it doesn t run the function Functions are only run when they are called A very small program might not define any functions at all, but just be a series of statements to be executed Most programs consist of a lot of function definitions, along with maybe a few top-level statements (statements not in functions) Usually one particular function is the starting point for running the program By convention, this function is usually named main, and usually it has no parameters Often a call to main() is the last top-level statement in a program def main(): print('hello!') main()

Defining and calling functions Simple Python programs are just a collection of functions, where all the functions are top-level, that is, not included in something else The syntax for defining a function is def function_name(parameter1,..., parametern): Comment describing the function one or more statements Inside a function, you can have one or more statements of the form return value or just return (with no value) The syntax for calling a function is function_name(argument1,..., argumentn) Usually, you should call a function with as many arguments as the function has parameters Usually, you should do something with the value returned from the function (save it in a variable, or use it in an expression or test) If the function has no parameters, the () parentheses are still required

Scope I The scope of a name is the part of the code in which other uses of that name refer to the same thing A default is what you get if you don t specify otherwise By default, the scope of a variable name is local to the function in which it occurs Every use of a name inside a function refers to the same thing If the same name is used in two different functions, they refer to two different things def quadruple(x): return double(x) + double(x) def double(x): return x + x x = 10 print(quadruple(x)) # prints 40

Example function def average(x, a): Averages two numbers. return (x + a) / 2 Suppose a = 7 and b = 10, and you execute the statement avg = average(a, a + b) Here s what happens: 1. The value 7 (from a) gets assigned to the function s x. 2. The value 17 (from a + b) gets assigned to the function s a. 3. The function computes and returns the value 12.0. 4. The value 12.0 is assigned to avg. (outside the function, a is still 7, and b is still 10). Notice that the a inside the function is different from the a outside the function. Notice that the values of the arguments (a and a + b) are assigned to the function s parameters (x and a different a), but the parameters are not assigned back to the arguments

Returning a result Every function returns a result There are three ways to return from a function: 1. Say return value -- the result of the function is value 2. Say return -- the result of the function is None 3. Reach the end of the function -- the result is None None is a special uninteresting value, but it is a value-- you can assign it, compare something to it, etc. Some functions, such as print and append, return None When you execute a return statement from within a loop, you don t finish the loop; the function is finished

A function is pure if - Pure functions the only information it gets is from its parameters, and the only information it produces is the value it returns Functions that get input or write output are not pure Functions that call impure functions are not pure (but they can call other pure functions) Functions that use global variables (next slide) are not pure Pure functions, being self-contained, add less to the overall complexity of a program than impure functions Rule: Minimize the number of impure functions

Global variables I A global variable is one whose scope is the entire program or entire module (file) Global variables are sometimes used for very important values used in many places For example, if you were writing a chess playing program, you might make the chessboard global Any function that uses global variables becomes dependent on whatever other functions might have done to those global variables Consequently, global variables can complicate debugging and understanding of programs Rule: Global variables should be used sparingly, if at all

Global variables II You can make a variable global by giving it a value outside any function: foo = 77 def main(): print(foo) # prints 77 You can make a variable global by declaring it to be global and giving it a value inside a function def bar(): global foo foo = 33 def main(): bar() print(foo) # prints 33 main() You cannot print(foo) before the call to bar() makes it global

Global variables III You don t need to do anything special to read (use the value of) a global variable within a function foo = 77 def main(): print(foo) # prints 77 However, if you assign to a variable with the same name as a global variable, that is a new local variable (the scope is the function) foo = 77 def main(): foo = 1 print(foo) # prints 1 main() # calls main, which prints 1 print(foo) # prints 77 If you want to assign a global variable in a function, rather than to a local variable with the same name, you must declare it inside that function to be global

Nested functions In Python, you can declare a function inside another function (making it local to the outer function) def average(x, y): def sum(x, y): return x + y return sum(x, y) / 2 The only reason you would do this is if the inner function is so specialized that it would have no use elsewhere In the inner function, you can get the values of variables in the enclosing function def average(x, y): def sum(y): return x + y return sum(x) / 2 If this seems to you like poor coding, I agree!

Nonlocal variables In a nested function, you can get the values of variables in the enclosing function......but if you assign to a variable, it s local To be able to assign to a variable in the enclosing function, you need to declare it nonlocal def average(x, y): sum = 0 def add(x, y): nonlocal sum sum = x + y add(x, y) # this call changes sum return sum / 2

Default arguments You can supply default values for the last parameter or parameters in a function def countdown(start=10): for i in range(start, 0, -1): print(i) print("blast off!") The range function is defined with a default step size of one: def range(start, stop, step=1):... Since arguments are matched to parameters by position, You can t put a parameter with a default value before one without a default value You can t use the default value for one parameter and supply a value for a later parameter

Named arguments In a function call, you can use names instead of position to match up arguments to parameters def foo(a, b, c): print('a =', a, ' b =', b, ' c =', c) foo(b=2, a=1, c=3) prints a = 1 b = 2 c = 3 You can even mix named and positional arguments, but the named ones must come first

Functions are values Functions are values, just like floats and booleans are values When you define a function, the name of the function is a variable whose value is that function Example: def double(x): return x + x twice = double print(twice(5)) # prints 10 def do_it(f, n): print(f(n)) print(do_it(double, 7)) # prints 14

Lambdas If a function (1) can be expressed as a single expression and (2) is only needed in a single place, then it can be written as a lambda expression Syntax: lambda arg_1,..., arg_n: expression Example: import math def do_and_show(fun, a, b): print(fun(a, b)) do_and_show(lambda x, y: math.sqrt(x * x + y * y), 3, 4) Lambda expressions are sometimes called anonymous functions, because a typical use is as a parameter to some other function However, you can certainly give a name to a lambda expression Example: hyp = lambda x, y: math.sqrt(x * x + y * y) print(hyp(3, 4)) # prints 5.0

Extended example One assignment requires a number of functions such as is_prime and is_square My test code contains the following method: def tell_about(self, fun, n, should_be): if fun(n) == should_be: return if should_be: self.fail(str(n) + " should be " + fun. name [3:]) else: self.fail(str(n) + " should not be " + fun. name [3:]) And uses calls to this method such as the following: self.tell_about(is_prime, 3, True) self.tell_about(is_prime, 4, False) self.tell_about(is_square, 3, False) self.tell_about(is_square, 4, True)

Generators When a function executes a return statement, that s it--the function is done But there s a way to leave a function, then come back to it at the same place you left off Use yield instead of return in the function Call the function once with any starting parameters you need, and save the result in a variable Call next(variable) for the first and each successive result Example: def powers(n): base = n yield n while True: n = base * n yield n gen = powers(2) for i in range(0, 11): print(next(gen))

The End There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. ---C.A.R. Hoare, 1981 Turing Award lecture