Function Basics - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Function Basics

Description:

In simple terms, a function (subroutine, procedure) is a package of ... Name resolution: the LEGB Rule: name references search at most at fours scopes: Local ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 25
Provided by: csg3
Learn more at: https://www.cs.gsu.edu
Category:
Tags: basics | fours | function

less

Transcript and Presenter's Notes

Title: Function Basics


1
Function Basics
2
Function
  • In this chapter, we will move on to explore a set
    of additional statements that create functions of
    our own
  • 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.

3
Why Function?
  • Functions serve two primary development roles
  • Code Reuse Functions allows us to group
    and generalize code to be used arbitrarily many
    times after it is defined
  • Procedure decomposition Functions also
    provide a tool for splitting systems into
    pieces---one function for each subtask

4
def statement
  • The def statement creates a function object and
    assigns it to a name. the def general format
  • def ltnamegt ( )
  • ltstatementsgt
  • the statement block becomes the functions
    body---the code Python executes each time the
    function is called

5
Function Example
  • def star()
  • numint(raw_input("please input a number"))
  • for i in range(num1)
  • print '' i
  • star() we call star function once

6
Function Example
  • def star()
  • numint(raw_input("please input a number"))
  • for i in range(num1)
  • print '' i
  • for i in range(3)
  • star() we call star function 3 times here

7
def statement
  • Now we try to see the def format with arguments
  • def ltnamegt(arg1, arg2, , argN)
  • ltstatementgt

8
Function Example with one Argument
  • def star(num)
  • for i in range(num1)
  • print '' i
  • for i in range(3)
  • num numint(raw_input("please input a
    number"))
  • star(num) we call star function 3 times
    here

9
Function Example with 2 arguments
  • def multiply(x,y)
  • valuexy
  • print value
  • multiply(4,10)
  • multiply(3,6)
  • multiply(2,18)

10
Function Example with 3 arguments
  • def multiply(x,y,z)
  • valuexyz
  • print value
  • multiply(4,10,1) generate result 40
  • multiply(4,10) error message multiply()
    takes exactly 3 arguments
  • (2 given)

11
def statement
  • Now we try to see the def format with arguments
    and return function
  • def ltnamegt(arg1, arg2, , argN)
  • return ltvaluegt

12
Function Example
  • def times(x,y)
  • valuexy
  • return value
  • aa times(2,4) aa8
  • aa times(3.14,4)aa12.56
  • aa times(ha,4)aahahahaha
  • list1,2,3,4
  • aa times(list,2) aa1,2,3,4,1,2,3,4

13
Function Example
  • def times(x,y)
  • for i in range(len(x))
  • xixiy
  • aa1,2,3,4
  • times(aa, 2)

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

15
Scope Basics
  • 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.
  • Name resolution the LEGB Rule name references
    search at most at fours scopes
  • Local
  • Enclosing functions (if any)
  • Global
  • Built-in.

16
Global Statement
  • Global names must be declared only if the
    assigned in a function.
  • Global names may be referenced in a function
    without being declared.
  • global ltname1,name2,nameNgt
  • gtgtgtX88
  • gtgtgtdef func()
  • global X
  • X99
  • gtgtgtfunc()
  • gtgtgtprint X

17
Passing Arguments
  • Arguments are passed by automatically assigning
    objects to a local names.
  • Assigning to arguments names inside a function
    doesnt affect the caller (by value).
  • Changing a mutable object argument in a function
    may impact a caller (by pointer).
  • gtgtgtdef change(x, y)
  • x2 y0hi
  • gtgtgtx1L1,a
  • gtgtgtchange(x,L)
  • gtgtgtprint x,L

18
Simulate Output Parameters
  • return sends back any sort of object
  • It could return multiple valus, by packaging them
    in tuple.
  • gtgtgtdef swap(x,y)
  • yxy
  • xy-x
  • yy-x
  • return x,y
  • gtgtgta,bswap(3,5)
  • gtgtgtprint a,b

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

20
Arguments Matching Modes
  • Positional matched left to right
  • def func(name)
  • func(value)
  • Keywords matched by the argument name
  • def func(name)
  • func(namevalue)
  • Varargs catch unmatched arguments.
  • def func(name) match remaining positional
  • args(in a tuple)
  • def func(name) match remaining keyword
  • args(in a dictionary)
  • Defaults specify values for arguments that are
    not passed
  • def func(namevalue)

21
Keyword Examples
  • gtgtgt def f(a,b,c) print a,b,c
  • gtgtgtf(1,2,3) by position
  • gtgtgtf(c3,b2,a1) keyword args
  • gtgtgtf(1,c3,b2) mix positional and keyword
    args

22
Keyword and Default Examples
  • Keywords
  • Self-documenting
  • In conjunction with defaults
  • gtgtgtdef f(a,b2,c1)print a,b,c
  • gtgtgtf(1)
  • gtgtgtf(a1)
  • gtgtgtf(2,4)
  • gtgtgtf(2,4,5)
  • gtgtgtf(2,c6)

23
Arbitrary Arguments Examples
  • Collect unmatched positional arguments into a
    tuple
  • gtgtgtdef f(args) print args
  • gtgtgtf()
  • gtgtgtf(1)
  • gtgtgtf(1,2,3,4)
  • Collect unmatched keyword arguments into a
    dictionary
  • gtgtgtdef f(args) print args
  • gtgtgtf()
  • gtgtgtf(a1,b2)
  • gtgtgtf(b1,c2,a3,d4)

24
Flexible Signature
  • gtgtgtdef f(a,pargs,krags) print a,pargs,krags
  • gtgtgtf(1,2,3,x1,y234)
Write a Comment
User Comments (0)
About PowerShow.com