Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015.

Slides:



Advertisements
Similar presentations
Python Programming: An Introduction to Computer Science
Advertisements

Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Python Programming: An Introduction to Computer Science
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
FUNCTIONS in Python.
Module 12 Computation and Configurations Formal Definition Examples.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
CS 201 Functions Debzani Deb.
Introduction to Methods
More Functions CS303E: Elements of Computers and Programming.
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
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.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
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.
CPS120: Introduction to Computer Science Functions.
CSC 110 Defining functions [Reading: chapter 6] CSC 110 G 1.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Vahé Karamian Python Programming CS-110 CHAPTER 6 Defining Functions.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
User defined functions
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Xiaojuan Cai Computational Thinking 1 Lecture 2 Simple Programs Xiaojuan Cai (蔡小娟) Fall, 2015.
Building java programs, chapter 3 Parameters, Methods and Objects.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Xiaojuan Cai Computational Thinking 1 Lecture 7 Decision Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
1 ICS103 Programming in C Lecture 8: Functions I.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Functions Functions, locals, parameters, and separate compilation.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Review Expressions and operators Iteration – while-loop – for-loop.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
CMSC201 Computer Science I for Majors Lecture 13 – Functions
CMSC201 Computer Science I for Majors Lecture 09 – Functions
Python Programming: An Introduction to Computer Science
A Lecture for the c++ Course
Chapter 6 Functions.
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
User-Defined Functions
CMSC201 Computer Science I for Majors Lecture 10 – Functions
CMSC201 Computer Science I for Majors Lecture 14 – Functions (Continued) Prof. Katherine Gibson Based on concepts from:
Lesson 06: Functions Class Chat: Attendance: Participation
5. Functions Rocky K. C. Chang 30 October 2018
九月九日憶山東兄弟 ~王維 獨在異鄉為異客, 每逢佳節倍思親。 遙知兄弟登高處, 遍插茱萸少一人。
15-110: Principles of Computing
答題分佈 Python Programming, 2/e.
Python Functions.
九月九日憶山東兄弟 ~王維 獨在異鄉為異客, 每逢佳節倍思親。 遙知兄弟登高處, 遍插茱萸少一人。
Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0.
Presentation transcript:

Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015

Xiaojuan Cai Computational Thinking 2 Objective To understand why using functions. To be able to define new functions. To understand the details of function calls and parameter passing. To write programs that use functions to reduce code duplication and increase program modularity.

Xiaojuan Cai Computational Thinking 3 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

Xiaojuan Cai Computational Thinking 4 What is function? A function is like a subprogram, a small program inside of a program. The basic idea: write a sequence of statements give that sequence a name (definition) execute this sequence at any time by the name (function call).

Xiaojuan Cai Computational Thinking 5 Functions so far Our programs comprise a single function called main(). Built-in Python functions ( abs ) Functions from the standard libraries ( math.sqrt ) Functions from the graphics module ( p.getX() )

Xiaojuan Cai Computational Thinking 6 Motivating example Happy Birthday lyrics … def main(): print "Happy birthday to you!" print "Happy birthday to you!" print "Happy birthday, dear Fred..." print "Happy birthday to you!" Remove duplicates: def happy(): print "Happy birthday to you!“

Xiaojuan Cai Computational Thinking 7 Motivating example Using parameters def sing(person): happy() happy() print "Happy birthday, dear", person + ".“ happy() A paramater is a variable that is initialized when the function is called.

Xiaojuan Cai Computational Thinking 8 The function of functions Having similar or identical code in more than one place has some drawbacks: writing the same code twice or more. this same code must be maintained in two separate places. Functions can be used to reduce code duplication and make programs more easily understood and maintained.

Xiaojuan Cai Computational Thinking 9 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

Xiaojuan Cai Computational Thinking 10 Functions, formally A function definition looks like this: def ( ): The name of the function must be an identifier Formal-parameters is a possibly empty list of variable names. def func(x): y = x * x return y a = func(2)

Xiaojuan Cai Computational Thinking 11 Function calls ( ) Four-step process: The calling program suspends at the point of the call. The formal parameters of the function get assigned (by position). The body of the function is executed. Control returns to the suspended point.

Xiaojuan Cai Computational Thinking 12 Function calls illustration

Xiaojuan Cai Computational Thinking 13 Future value with a function Duplicated codes for drawing bars. To properly draw the bars, we need three pieces of information. The year the bar is for How tall the bar should be The window the bar will be drawn in These three values can be supplied as parameters to the function.

Xiaojuan Cai Computational Thinking 14 drawBar function def drawBar(window, year, height): bar = Rectangle(Point(year, 0), Point(year+1, height)) bar.setFill("green") bar.setWidth(2) bar.draw(window) Why send the window variable as param? Since the GraphWin in the variable win is created inside of main, it is not directly accessible in drawBar.

Xiaojuan Cai Computational Thinking 15 Scoping rule The scope of a variable refers to the places in a program a given variable can be referenced. The variables used inside of one function are local to that function. The only way to see a variable from another function is passing it as a parameter. x,y = 0,0 def f(x): y = 1 print x,y f(10) print x,y

Xiaojuan Cai Computational Thinking 16 Global variables >>> x = 1 >>> def f(): print x x = 2 >>> f() >>> def h(): global x print x x = 2 print x >>> h() f() x h() x x

Xiaojuan Cai Computational Thinking 17 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

Xiaojuan Cai Computational Thinking 18 Return values Params are “inputs” of a function Return values are “outputs” of a function discRt = math.sqrt(b*b – 4*a*c) def square(x): return x*x Functions without a return hand back a special object, denoted None. Triangle2.py

Xiaojuan Cai Computational Thinking 19 Modifying params Sometimes, we can communicate back to the caller by making changes to the function parameters. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount

Xiaojuan Cai Computational Thinking 20 What went wrong?

Xiaojuan Cai Computational Thinking 21 What went wrong?

Xiaojuan Cai Computational Thinking 22 The same problem as alias The formal parameters of a function only receive the values of the actual parameters.

Xiaojuan Cai Computational Thinking 23 Roadmap Functions, informally Function definition and call Getting results from functions Return values Modify params Functions and program structure

Xiaojuan Cai Computational Thinking 24 Program structure So far, functions have been used as a mechanism for reducing code duplication. Another reason to use functions is to make your programs more modular. (Even though the amount of code increases) Example: 99-bottle-of-wine

Xiaojuan Cai Computational Thinking 25 Conclusion A function is a kind of subprogram. A call to a function initiates a four-step process. 1. caller suspend, 2. assign the formal params, 3. execute the callee, 4. return to the caller The scope of a variable is where it can be referenced. Functions can return values. Python passes parameters by value.