Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Similar presentations


Presentation on theme: "Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License."— Presentation transcript:

1 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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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.

11 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

12 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

13 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.

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

15 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.

16 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

17 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.

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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.

28 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

29 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

30 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

31 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


Download ppt "Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License."

Similar presentations


Ads by Google