Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Computer Programming w/ Eng. Applications
Kernighan/Ritchie: Kelley/Pohl:
CS0004: Introduction to Programming Repetition – Do Loops.
Introduction to C Programming
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Introduction to C Programming
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Announcements Class / Recitation slides available on the syllabus page Check morning of for latest version We will start grading clickers next lecture.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Announcements Project 2 Available Tomorrow (we will send mail) Will be due 11:59PM October 9 th (Sunday) Week 6 ( I will be traveling this week) Review.
Munster Programming Training
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Functions and subroutines – Computer and Programming.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Announcements Additional office hours tomorrow: 3-6pm in HAAS 142 Midterm Bring a #2 pencil (test given via scantron sheet) Photo ID No Labs or Recitation.
Topics: IF If statements Else clauses. IF Statement For the conditional expression, evaluating to True or False, the simple IF statement is if : x = 7.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
More Functions with Return Values CS303E: Elements of Computers and Programming.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Announcements Survey link (can be found on piazza): OCJ6
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
1 CS Review, iClicker -Questions Week 15. Announcements 2.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Announcements Reading Read Chapter 4 (functions).
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Announcements Reading Project1 Codelab: Read Chapter 4 (functions)
Chapter 9: Value-Returning Functions
Functions CIS 40 – Introduction to Programming in Python
Functions.
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
CMSC201 Computer Science I for Majors Lecture 14 – Functions (Continued) Prof. Katherine Gibson Based on concepts from:
5. Functions Rocky K. C. Chang 30 October 2018
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
答題分佈 Python Programming, 2/e.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Presentation transcript:

Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the lab machines Midterm 1 October 6 th 2011 WTHR 200 Midterm 2 November 8 th 2011 WTHR 200

Project 1 You will be tasked with writing function to calculate: the velocity need to hit a pig at a given distance and elevation with a certain angle.

Project 1 You will be tasked with writing function to verify the trajectory using the information you calculated in the previous function. Did rounding affect the calculation?

Project 1 You will be tasked with displaying some of the information you calculate graphically and “gluing” the various parts together in one larger “main” function.

Python Boot Camp! Multiple Arguments Returns Passing a value out of a function (output) Last statement executed Scope Local Variables Comments / Coding Practices

Indentation Indentation is very important in python In functions it defines where the function ends Python will give errors if the indentation is incorrect

Multiple Arguments Functions can take multiple arguments def sumOfTwo(a,b): return a+b def sumOfThree(a,b,c): return a+b+c def sumOfFour(a,b,c,d): return a+b+c+d

Call Sites You must provide the number of arguments the function expects def sumOfTwo(a,b): return a+b print(sumOfTwo(4,5)) def sumOfThree(a,b,c): return a+b+c print(sumOfThree(3,3,3)) def sumOfFour(a,b,c,d): return a+b+c+d print(sumOfFour(3,3,3)) ERROR!

Clicker Question: Are these programs equivalent? def myFun(a): print (a) return 5 print(myFun(4)) def myFun(a): print (a) return 5 res = myFun(4) print(res) 21 A: yes B: no

“Capturing” the return The return statement defines the value that is passed out from the function We can store the return value in a variable def sumOfTwo(a,b): return a+b result = sumOfTwo(4,5)) def sumOfThree(a,b,c): return a+b+c result = sumOfThree(3,3,3)

Stringing together functions We can build up complex expressions from functions def sumOfTwo(a,b): return a+b print(sumOfTwo(sumOfTwo(4,5), sumOfTwo(1,2))) OR a = sumOfTwo(4,5) b = sumOfTwo(1,2) c = sumOfTwo(a,b) print(c)

A “Sample” Program def task1(a,b): … y = …. return y def task2(a,b,c): …. z = … return z def main(): input1, input2 = eval(input(…)) output1 = task1(input1, input2) output2 = task2(input1, input2, output1) print(output2)

More on Returns Returns are the last statement executed by a function The function is considered to be “done” at this point There may be other statements “after” the return, but these are omitted def sumOfTwo(a,b): return a+b print(a+b) print(sumOfTwo(4,5))

Clicker Question: Are these programs equivalent? a = 3 def myFun(a): print (a) myFun(4) a = 3 print (a) 21 A: yes B: no

Naming and Binding Arguments are bound at the “call site” Call site: a point in the program where the function is called The binding is active up until the return of the function

Naming and Binding a = 3 def myFun(a): print (a) myFun(4) print(a)

Local Variables We saw that arguments can be rebound for the duration of the call What about variables that we define in the function body?

Local Variables Abstractly there are two types of local variables 1) those which introduce a new variable 2) those which redefine an existing variable which was initially defined outside the function definition Local variables, just like arguments, have a lifetime associated with the function call

Local Variables a = 3 y = 10 def myFun(a): print (a) y = 1 myFun(4) print(a) print(y)

Clicker Question: does this program print 3 or 4? x = 3 def myFun(): print (x) x = 4 myFun() A: 3 B: 4

Homework Work on Project 1 Read chapter 6 (functions)

Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the lab machines PreLab 4 Important! Practice Lab 4 Graphics Library methods

Python Boot Camp! Local vs Global Variables Comments / Coding Practices

Variables and Functions Variables used in functions but defined outside of the function can be changed Multiple calls to a function may yield different results if the program “rebinds” such variables

Variables and Functions x = 3 def myFun(): print (x) x = 4 myFun() x =5 myFun()

You must be careful! x = 3 def myFun(): print (x) x =1 x = 4 myFun() x =5 myFun() ERROR!

Global Variables How can we get the example code we saw earlier to work? Python is not sure if we want x to be a local variable or if it should refer to the x we defined outside of the function We can inform python if we want x to refer to the variable outside of the function New keyword global

This works! x = 3 def myFun(): global x print (x) x =1 x = 4 myFun()

Global or local? If the global keyword is use, the variable is global If the first use of the variable is a read the variable is global NOTE: We cannot assign to the variable The variable is not defined in the arguments If the first use of the variable is a write (assignment) the variable is local Unless the variable is defined global

Clicker Question: Is X global or local? x = 3 def myFun(): y = 4 z = x + y myFun() A: global B: local

Lets Review Functions take input and produce output Output is provided by the “return” statement Otherwise the function does not provide output At the call site of the function the arguments get bound The arguments can rebind variables that have already been defined for the duration of the call You can use variables defined outside the function but you must be careful!

Function Arguments Functions typically assume something important about the arguments Will this work no matter what we provide as arguments? def sumOfTwo(a,b): return a+b

Function Arguments Consider the following three cases: One of these cases will throw an error. This behavior is defined by the code inside the function res = sumOfTwo(1,2) res = sumOfTwo(“Hello “, “World”) res = sumOfTwo(“Hello”, 1)

Function Arguments There are two ways to handle this difficulty 1. Tell everyone what the function expects 2. Include checks inside the function to ensure the arguments are what is expected A combination of both techniques should be used

Function Documentation This solution uses comments and if-statements. We will revisit this in later slides #This function expects two integers and returns -1 def sumOfTwo(a,b): if (type(a) == int and type(b) == int) : return a+b return -1

Functions that Modify Parameters Suppose you are writing a program that manages bank accounts. One function we would need to do is to accumulate interest on the account. Let’s look at a first- cut at the function. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance

Functions that Modify Parameters The intent is to set the balance of the account to a new value that includes the interest amount. Let’s write a main program to test this: def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount)

Functions that Modify Parameters We hope that that the 5% will be added to the amount, returning >>> test() 1000 What went wrong? Nothing!

Functions that Modify Parameters The first two lines of the test function create two local variables called amount and rate which are given the initial values of 1000 and 0.05, respectively. def addInterest(balance, rate): newBal = balance*(1+rate) balance = newBal def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount)

Functions that Modify Parameters

def addInterest(balance, rate): newBalance = balance * (1 + rate) return newBalance def test(): amount = 1000 rate = 0.05 amount = addInterest(amount, rate) print(amount) test()

Functions that Modify Parameters amount = 1000 rate = 0.05 def addInterest(): global amount global rate amount = amount * (1 + rate) addInterest() print(amount)

What all can I do within the body of a function? Define local variables Call other functions Define new functions

Homework Work on Project 1 Do the pre lab / review chapter 4 Read Chapter 6 Read Python Wiki “Functions” ions ions