python functions functions
play

Python: Functions Functions Mathematical functions f(x) = x 2 - PowerPoint PPT Presentation

Python: Functions Functions Mathematical functions f(x) = x 2 f(x,y) = x 2 + y 2 In programming functions also help creating better structure with decomposition Functions


  1. Python: Functions

  2. Functions Mathematical functions f(x) = x 2 f(x,y) = x 2 + y 2 In programming functions also help creating better structure with decomposition

  3. Functions Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  4. Defining and invoking a function Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  5. Defining and invoking a function Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  6. Defining and invoking a function Consider f(x) = x 2 def square(x): #defining function return x*x square(4) #invoking function 16 # output

  7. Defining and invoking a function Example: Functions may not have arguments, and return statement def myprint(): #defining function print (“Hello world”) myprint() #invoking function Hello world # output

  8. Defining and invoking a function Example: Function calling another function def repeatmyprint(): myprint() myprint() repeatmyprint() #invoking function Hello world # output Hello world

  9. Scope of a Variable Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  10. Scope of a Variable Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  11. Scope of a Variable returns 4 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  12. Scope of a Variable Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  13. Function: Arguments No argument ( ) One argument ( ) One argument (function) ( ) Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  14. Function: Arguments ( ) ( ) ( ) ( ) ( ) ( ) Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  15. Function: Arguments ( ) ( ) ( ) ( ) ( ) ( ) Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  16. Function: Arguments ( ) ( ) ( ) ( ) ( ) ( ) Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  17. Function: Arguments ( ) ( ) Output ( ) ( ) inside func_a None ( ) ( ) Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  18. Function: Arguments ( ) ( ) Output ( ) ( ) inside func_b ( ) 7 ( ) Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  19. Function: Arguments ( ) ( ) Output ( ) ( ) ( ) inside func_c ( ) inside func_a None Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  20. Function: Scope x is redefined locally Output 2 5 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  21. Function: Scope Can access x defined outside Output 5 6 5 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  22. Function: Scope Can not modify x defined outside Output UnboundLocalError Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

  23. Function: Scope (Example) Output g:x=4 print (z) 4 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend