310 likes | 411 Views
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.
E N D
Chapter 5 Functions
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.
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.
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:
Scope A visualization of the three principle scopes in Python - built-in identifiers form the outermost scope followed by global identifiers and function identifiers.
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.
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.