PPTX SD - Problemspace

Download Report

Transcript PPTX SD - Problemspace

Chapter 5
Functions
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Overview
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Function Declaration
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Parameters
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Parameters
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Positional Arguments
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Keyword Arguments
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Default Arguments
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Return Values
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
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.
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Docstrings
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Docstrings
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
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.
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Scope
Python has three different kinds of scope:
1. the built-in scope, which is made up identifiers defined by Python,
2. the global scope, which is made up identifiers defined in a Python file but not
within a function, and
3. 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:
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Scope
A visualization of the three principle scopes in Python - built-in identifiers form the
outermost scope followed by global identifiers and function identifiers.
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Scope
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
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.
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Scope
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Code Repetition
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Code Repetition
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Code Repetition
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Code Repetition
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Top-Down Design
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Top-Down Design
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Top-Down Design
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Top-Down Design
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Top-Down Design
1. If we break the problem up into smaller parts,
we clarify the tasks that need to be done to
solve the problem.
2. Each smaller part is typically less complicated
than the whole, making the problem more
approachable.
3. Smaller parts may be reusable, reducing the
implementation size.
4. Breaking the problem up also allows us to
distribute the problem solving work, enabling
team work.
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Recursion
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Recursion
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Functions as Values
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Functions as Values
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.