Chapter 12-13 Function Basics CSC1310 Fall 2009. Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Chapter 5 Functions.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 6: User-Defined Functions I
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6: User-Defined Functions I
C++ for Engineers and Scientists Third Edition
Chapter 15: Operator Overloading
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
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,
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CPS120: Introduction to Computer Science Decision Making in Programs.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Chapter 14 Advanced Function Topics CSC1310 Fall 2009.
Python Functions.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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,
Chapter 3: User-Defined Functions I
Chapter 10 Loops: while and for CSC1310 Fall 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 6: User-Defined Functions I
Methods Chapter 6.
Principles of programming languages 4: Parameter passing, Scope rules
Organization of Programming Languages
Functions.
Programmazione I a.a. 2017/2018.
User-Defined Functions
CHAPTER FOUR Functions.
Group Status Project Status.
Chapter 6: User-Defined Functions I
G. Pullaiah College of Engineering and Technology
COMPUTER PROGRAMMING SKILLS
The Three Attributes of an Identifier
Parameters and Arguments
Learning Python 5th Edition
Presentation transcript:

Chapter Function Basics CSC1310 Fall 2009

Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In simple terms, a function (subroutine, procedure) is a package of code (a set of statements) that can be called repeatedly with different inputs (parameters) and outputs each time. an object In Python, function is an object which is recorded explicitly at program execution time.

Why Use Function? 1. Code Reuse: 1. Code Reuse: Functions allows us to group and generalize code to be used arbitrarily many times after it is defined. 2. Procedural decomposition: 2. Procedural decomposition: Functions also provide a tool for splitting systems into pieces that have a well-defined role (one function for each subtask).

def Statement def The def statement creates a function object and assigns it to a name. General format: def (arg1,arg2, … argN): <statements> return return body Python executes body (statement block) each time the function is called. at the point of a call The argument names will be assigned with the objects passed in the parenthesis at the point of a call. return return ends function call and sends a result back to a caller.

def Runs at Runtime def The def is true executable statement:  Creates and assigns a new function object to a name at a runtime  It is a statement: it can be even nested. >>>k=int(raw_input()) >>>if k==1: def func(): print ”One” else: def func(): print “Not one” >>>print func() def The def is like an = statement. >>>othername=func >>print othername()

Function Example Definition Definition (def) creates a function Call Call – an expression to run the function’s body >>>def pow(x,y): value=x+y return value >>>print pow(2,3) typeless >>>print pow(‘abc’,’345’) # functions are typeless

Polymorphism Polymorphism Polymorphism is a type-dependent behavior: the meaning of operations depends on the objects being operated upon. interface As long as objects support the expected protocol (interface), they can be processed by the function (expected methods and operators, compatible with function’s logic). do not support interface If objects do not support interface, Python raises an exception automatically. is not supposed code to object interfaces Python vs Java, C++: your code is not supposed to care about specific data types (we code to object interfaces, not data type.)

Example: Intersecting Sequences >>>def intersect(x,y): local variable res=[] # local variable for z in x: if z in y: res.append(z) return res You can run code as many times as you want. It is general to work on any two sequences. You need to change code only in one place. It can be imported and reused by any other program.

Intersecting Sequences: Call >>>s1,s2=‘Wiki’, ’wiki’ >>>intersect(s1,s2) >>>print intersect([1,2,3], (1,4)) >>>print intersect([1,2,3], (4))

Scope Rules Namespace Namespace is the place where names live scope The location of name’s assignment defines the scope of the name visibility. def def Names defined inside a def can be seen only by the code inside the def. def def Names defined inside a def not clash with variables outside the def, even if the same name is used elsewhere.

Scope Basic The enclosing module is a global scope. The global scope spans a single file only. Each call to a function is a new local scope. Assigned names are local, unless declared global. All names are either local, or global, or built-ins. LEGB Rule: Name resolution: the LEGB Rule: name references search at most at fours scopes:  Local  Enclosing functions (if any)  Global  Built-in.

global Statement Global names must be declared only if used in a function. Global names may be referenced in a function without being declared. global global >>>X=88 >>>def func(): global X X=99 >>>func() >>>print X

Nested Functions Within a function:  Assignment: X=value global  Assignment: X=value creates or changes name x in the current local scope by default. If X is declared global within a function, it creates or changes X in the enclosing module’s scope instead.  Reference: X global  Reference: X looks for a name in the current local scope (function), then in the local scopes of all lexically enclosing functions from inner to outer (if any), then in the current global scope (the module file), and finally in built-in scope. global declaration makes the search begin in the global scope instead.

Passing Arguments Arguments are passed by automatically assigning objects to a local names. (“by value”) Assigning to arguments’ names inside a function doesn’t affect the caller (“by value”). (“by pointer”) Changing a mutable object argument in a function may impact a caller (“by pointer”). >>>def change(x, y): x=2; y[0]=‘hi’ >>>x=1;L=[1,’a’] >>>change(x,L) >>>print x,L

Avoiding Mutable Arguments Changes We can copy the list at the point of the call >>>L=[1,’a’] L[:] >>>change(x,L[:]) # pass a copy We can copy list within the function itself, if we never want to change passed-in objects. >>>def change(x, y): y=y[:] x=2; y[0]=‘hi’ Both variants above only prevent changes from impacting a caller. We can convert to immutable objects. >>>L=[1,’a’] tuple(L) >>>change(x,tuple(L)) # pass a tuple

Simulating Output Parameters return return sends back any sort of object It could return multiple values, by packaging them in tuple. >>>def swap(x,y): y=x+y x=y-x y=y-x return x,y >>>a,b=swap(3,5) >>>print a,b

Special Argument Matching Modes Positional: Positional: matched left to right. Keywords: name=value Keywords: matched by the argument name. Callers can specify which argument is to receive a value by specifying the argument’s name in the call with a name=value syntax. Varargs: * Varargs: catch unmatched positional or keyword arguments. Function can use special arguments preceded with a * characters to collect arbitrarily extra arguments. Defaults: name=value Defaults: specify values for arguments that are not passed using a name=value syntax.

Function Argument-Matching Forms Positional: Positional: matched left to right. def func(name) func(value) Keywords: Keywords: matched by the argument name. def func(name) func(name=value) Varargs: Varargs: catch unmatched arguments. def func(*name) # match remaining positional # args(in a tuple) # args(in a tuple) def func(**name) # match remaining keyword # args(in a dictionary) # args(in a dictionary) Defaults: Defaults: specify values for arguments that are not passed def func(name=value)

Keyword Examples >>> def f(a,b,c): print a,b,c >>>f(1,2,3) # by position >>>f(c=3,b=2,a=1) # keyword args >>>f(1,c=3,b=2) #mix positional and keyword args

Keyword and Default Examples Keywords Keywords:  Self-documenting  In conjunction with defaults >>>def f(a,b=2,c=1):print a,b,c >>>f(1) >>>f(a=1) >>>f(2,4) >>>f(2,4,5) >>>f(2,c=6)

Arbitrary Argument Examples positional tuple Collect unmatched positional arguments into a tuple: >>>def f(*args): print args >>>f() >>>f(1) >>>f(1,2,3,4) keyword dictionary Collect unmatched keyword arguments into a dictionary: >>>def f(**args): print args >>>f() >>>f(a=1,b=2) >>>f(b=1,c=2,a=3,d=4)

Flexible Signature >>>def f(a,*pargs,**krags): print a,pargs,kargs >>>f(1,2,3,x=1,y=‘234’)

Min Example Make a function that compute a minimum value from:  An arbitrary set of element (*args).  Arbitrary set of object datatypes(polymorphism). >>>def min1(*args): res=args[0] for arg in args[1:]: if arg<res: res=arg return arg >>>def min2(first,*rest): for arg in rest: if arg<first: first=arg return first >>>def min3(*args): tmp=list(args); tmp.sort(); return tmp[0]

Argument Matching keywordafter all non-keyword  In a call, all keyword arguments must appear after all non-keyword arguments. *nameafter normal arguments and defaults**name last  In a function header, *name must appear after normal arguments and defaults, **name must appear last.  Steps for assigning done by Python: 1. Assign non-keyword arguments by position. 2. Assign keyword args by matching names. 3. Assign extra non-keyword args to *name tuple 4. Assign extra keyword to **name dictionary. 5. Assign default values to unassigned args in header.