1 / 31

Chapter 5

Chapter 5. Functions. Overview . Function Declaration. Parameters. Parameters. Positional Arguments. Keyword Arguments. Default Arguments. Return Values. Return Values. If a return statement omits a return value, None is returned implicitly.

Download Presentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 Functions

  2. Overview

  3. Function Declaration

  4. Parameters

  5. Parameters

  6. Positional Arguments

  7. Keyword Arguments

  8. Default Arguments

  9. Return Values

  10. Return Values If a return statement omits a return value, None is returned implicitly. Likewise, if a function reaches the end of its evaluation without reaching a return statement, the function also returns None.

  11. Docstrings

  12. Docstrings

  13. Docstring Conventions As a matter of consistency always use triple quotes for docstrings even if it is a one line string, The docstring summary should begin with an action verb (e.g., “Return this..”, “Do that..”) The docstring summary should end with a period. For multi-line documentation, the docstring should begin with a one line summary, then one blank line, and then the longer documentation.

  14. Scope Python has three different kinds of scope: the built-in scope, which is made up identifiers defined by Python, the global scope, which is made up identifiers defined in a Python file but not within a function, and the function scope, which is made up identifiers (including parameters) defined in a function. Consider this simple example where these three different scopes are being used:

  15. Scope A visualization of the three principle scopes in Python - built-in identifiers form the outermost scope followed by global identifiers and function identifiers.

  16. Scope

  17. Scope In this example the function scope has a variable x which shadows the global scope. The result is two different variables that happen to have the same name.

  18. Scope

  19. Code Repetition

  20. Code Repetition

  21. Code Repetition

  22. Code Repetition

  23. Top-Down Design

  24. Top-Down Design

  25. Top-Down Design

  26. Top-Down Design

  27. Top-Down Design If we break the problem up into smaller parts, we clarify the tasks that need to be done to solve the problem. Each smaller part is typically less complicated than the whole, making the problem more approachable. Smaller parts may be reusable, reducing the implementation size. Breaking the problem up also allows us to distribute the problem solving work, enabling team work.

  28. Recursion

  29. Recursion

  30. Functions as Values

  31. Functions as Values

More Related