COMP1730/COMP6730 Programming for Scientists. Functions

Similar documents
COMP1730/COMP6730 Programming for Scientists. Functional abstraction, with robots

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Lecture 4. Defining Functions

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Lecture 4. Defining Functions

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

61A Lecture 2. Friday, August 28, 2015

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Functions with Parameters and Return Values

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Downloaded from Chapter 2. Functions

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 22, 2018

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

61A Lecture 2. Wednesday, September 4, 2013

Control Structures 1 / 17

COMP1730/COMP6730 Programming for Scientists. Control, part 2: Iteration

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CS1 Lecture 4 Jan. 24, 2018

Text Input and Conditionals

Lecture 4. Defining Functions

Lecture 5. Defining Functions

CS1 Lecture 4 Jan. 23, 2019

SCHEME AND CALCULATOR 5b

Python: Functions. Thomas Schwarz, SJ Marquette University

Introduction to MATLAB

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

CS1110. Lecture 6: Function calls

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

Built-in Types of Data

Scientific Computing: Lecture 3

Programming Training. Main Points: - Python Statements - Problems with selections.

Programming with Python

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Com S 127x - Lab 6 1. READING FLOWCHARTS WITH CONDITIONAL ACTIONS!

Math 144 Activity #4 Connecting the unit circle to the graphs of the trig functions

PREPARING FOR PRELIM 1

Lecture 4: Defining Functions

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Exponents. Common Powers

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

CMSC 201 Computer Science I for Majors

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

PROGRAMMING FUNDAMENTALS

CS 102 Lab 3 Fall 2012

Module 01: Introduction to Programming in Python

CS1 Lecture 5 Jan. 25, 2019

Introduction to MATLAB

Introduction to Python (All the Basic Stuff)

CS1110. Lecture 6: Function calls

Lecture 3. Input, Output and Data Types

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

THE AUSTRALIAN NATIONAL UNIVERSITY Final Examination November COMP1730 / COMP6730 Programming for Scientists

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

Functions and Decomposition

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Lecture-14 Lookup Functions

Lecture 3. Functions & Modules

ENGR 102 Engineering Lab I - Computation

CSE 115. Introduction to Computer Science I

COMP10001 Foundations of Computing Testing and Debugging

Scientific Computing: Lecture 1

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CS 1301 CS1 with Robots Summer 2007 Exam 1

Using IDLE for

Introducing Python Modules

A Problem. Loop-Body Returns. While-Loop Solution with a Loop-Body Return. 12. Logical Maneuvers. Typical While-Loop Solution 3/8/2016

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

Math 2250 Lab #3: Landing on Target

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

Use Parametric notation. Interpret the effect that T has on the graph as motion.

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

Lecture 3. Functions & Modules

Python 1: Intro! Max Dougherty Andrew Schmitt

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Unit E Step-by-Step: Programming with Python

Decisions, Decisions. Testing, testing C H A P T E R 7

CMSC 201 Fall 2018 Lab 04 While Loops

Question 1. CSC 120H1 F Midterm Test Solutions Fall 2017

CS 11 python track: lecture 2

Execution order. main()

What is an algorithm?

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

Intro. Scheme Basics. scm> 5 5. scm>

Introduction to Engineering gii

DM536 Introduction to Programming. Peter Schneider-Kamp.

What You ll See in This Chapter. Word Cloud. René Descartes. Introduction. Ian Parberry University of North Texas. Fletcher Dunn

Introduction to: Computers & Programming: Review prior to 1 st Midterm

DECOMPOSITION, ABSTRACTION, FUNCTIONS

Subroutines I Computers and Programming

ECS Baruch Lab 5 Spring 2019 Name NetID (login, like , not your SUID)

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Transcription:

COMP1730/COMP6730 Programming for Scientists Functions

Lecture outline * Function definition. * Function calls & order of evaluation. * Assignments in functions; local variables. * Function testing.

Functions * In programming, a function is a piece of the program that is given a name, and can be called by that name. * Functions promote abstraction ( what, not how ) and help break a complex problem into smaller parts. * To encapsulate computations on data, functions have parameters and a return value.

Function definition (reminder) name def change in percent(old, new): 4 diff = new - old spaces return (diff / old) * 100 suite * A function definition consists of a name and suite. * The extent of the suite is defined by indentation, which must be the same for all statements in the suite (standard is 4 spaces).

Function definition parameters def change in percent(old, new): diff = new - old return (diff / old) * 100 * Function parameters are (variable) names; they can be used (only) in the function suite. * Parameters values will be set only when the function is called. * return is a statement: when executed, it causes the function call to end, and return the value of the expression.

Function call * To call a function, write its name followed by its arguments in parentheses: >>> change in percent(364, 485) 33.24175824175824 * The arguments are expressions. * Their number should match the parameters. - Some exceptions; more about this later. * A function call is an expression: its value is the value returned by the function.

Order of evaluation * The python interpreter always executes instructions one at a time in sequence; this includes expression evaluation. * To evaluate a function call, the interpreter: - First, evaluates the argument expressions, one at a time, from left to right. - Then, executes the function suite with its parameters assigned the values returned by the argument expressions. * Same with operators: first arguments (left to right), then the operation.

import math # Convert degrees to radians. def deg to rad(x): return x * math.pi / 180 # Take sin of an angle in degrees. def sin of deg(x): x in rad = deg to rad(x) return math.sin(x in rad)

import math def deg to rad(x): return x * math.pi / 180 def sin of deg(x): x in rad = deg to rad(x) return math.sin(x in rad) answer = sin of deg(23) (Image from pythontutor.com)

import math def deg to rad(x): return x * math.pi / 180 def sin of deg(x): x in rad = deg to rad(x) return math.sin(x in rad) answer = sin of deg(23) (Image from pythontutor.com)

import math def deg to rad(x): return x * math.pi / 180 def sin of deg(x): x in rad = deg to rad(x) return math.sin(x in rad) answer = sin of deg(23) (Image from pythontutor.com)

import math def deg to rad(x): return x * math.pi / 180 def sin of deg(x): x in rad = deg to rad(x) return math.sin(x in rad) answer = sin of deg(23) (Image from pythontutor.com)

import math def deg to rad(x): return x * math.pi / 180 def sin of deg(x): x in rad = deg to rad(x) return math.sin(x in rad) answer = sin of deg(23) (Image from pythontutor.com)

The call stack * When evaluation of a function call begins, the current instruction sequence is put on hold while the expression is evaluated. * When execution of the function suite ends, the interpreter returns to the next instruction after where the function was called. * The to-do list of where to come back to after each current function call is called the stack.

1 import math 2 def deg to rad(x):... 3 def sin of deg(x):... 4 ans=sin of deg(23) 9 ans = 0.3907 5 x in rad=deg to rad(23) 7 x in rad=0.4014 8 return math.sin(0.4014) 6 return 23*math.pi/180 stack depth

Assignments in functions * Variables assigned in a function (including parameters) are local to the function. - Local variables are separate the interpreter uses a new namespace for each function call. - Local variables that are not parameters are undefined before the first assignment in the function suite. - Variables with the same name used outside the function are unchanged after the call. * The full story is a little more complicated we ll return to it later in the course.

Functions with no return * If execution of a function suite reaches the end of the suite without encountering a return statement, the function call returns the special value None. - None is used to indicate no value. - The type of None is NoneType (different from any other value). * In interactive mode, the interpreter does not print the return value of an expression when the value is None.

Side effects and return values * An expression evaluates to a value. * A statement does not return a value, but executing it causes something to happen, e.g., - a number = 2 + 3 : variable a number becomes associated with the value 5; - print(2 + 3) : the value 5 is printed. This is called a side effect. * We can write functions with or without side effects, and functions that do or don t return a value (other than None).

* Functions with side effects and no return value: - robot.drive right() - print(...) * Functions with return value and no side effect: - math.sin(x) - change in percent(old, new) * Functions with side effects and return value? - Possible. * Functions with no side effect and no return value?

Function testing * A function is a logical unit of testing. - Specify the assumptions (for example, type and range of argument values); - Test a variety of cases under the assumptions. * What are edge cases? - Typical (numeric) examples: values equal to/less than/greater than zero; very large and very small values; values of equal and opposite signs; etc. * Remember that floating-point numbers have limited precision; == can fail.

>>> change in percent(1, 2) 100.0 >>> change in percent(2, 1) -50.0 >>> change in percent(1, 1) 0.0 >>> change in percent(1, -1) -200.0 >>> change in percent(0, 1) ZeroDivisionError

The function docstring def change in percent(old, new): Return change from old to new, as a percentage of the old value. old value must be non-zero. return return ((new - old) / old) * 100 * A docstring is a string literal written as the first statement inside a function s suite. * Acts like a comment, but accessible through the built-in help system. * Describe what the function does (if not obvious from its name), and its limits and assumptions.