Top Banner
Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Chapter 5 Functions
31

Chapter 5

Dec 31, 2015

Download

Documents

James Patton

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. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Chapter 5

Functions

Page 2: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Overview

Page 3: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Function Declaration

Page 4: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Parameters

Page 5: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Parameters

Page 6: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Positional Arguments

Page 7: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Keyword Arguments

Page 8: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Default Arguments

Page 9: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Return Values

Page 10: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This 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.

Page 11: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Docstrings

Page 12: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Docstrings

Page 13: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This 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.

Page 14: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

ScopePython 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:

Page 15: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This 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.

Page 16: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Scope

Page 17: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This 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.

Page 18: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Scope

Page 19: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Code Repetition

Page 20: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Code Repetition

Page 21: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Code Repetition

Page 22: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Code Repetition

Page 23: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Top-Down Design

Page 24: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Top-Down Design

Page 25: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Top-Down Design

Page 26: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Top-Down Design

Page 27: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This 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.

Page 28: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Recursion

Page 29: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Recursion

Page 30: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Functions as Values

Page 31: Chapter 5

Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Functions as Values