# 4.2 Defining Functions¶

- `square` function that calculates the square of its argument.

In [1]:

    def square(number):
        """Calculate the square of number."""
        return number ** 2

In [2]:

    square(7)

Out[2]:

    49

In [3]:

    square(2.5)

Out[3]:

    6.25

- Calling `square` with a non-numeric argument like `'hello'` causes a `TypeError` because the exponentiation operator (`**`) works only with numeric values

### Defining a Custom Function¶

- Definition begins with the (`def` keyword, followed by the function name, a set of parentheses and a colon (`:`).
- By convention function names should begin with a lowercase letter and in multiword names underscores should separate each word.
- Required parentheses contain the function’s parameter list.
- Empty parentheses mean no parameters.
- The indented lines after the colon (`:`) are the function’s block
  - Consists of an optional docstring followed by the statements that perform the function’s task.

### Specifying a Custom Function’s Docstring¶

- Style Guide for Python Code: First line in a function’s block should be a docstring that briefly explains the function’s purpose.

### Returning a Result to a Function’s Caller¶

- Function calls also can be embedded in expressions:

In [4]:

    print('The square of 7 is', square(7))

    The square of 7 is 49

- Three Ways to Return a Result to a Function’s Caller
  - `return` followed by an expression.
  - `return` without an expression implicitly returns `None`—represents the absence of a value and evaluates to `False` in conditions.
  - No `return` statement implicitly returns `None`.

### What Happens When You Call a Function¶

- Parameters exist only during the function call.
- Created on each call to the function to receive arguments.
- Destroyed when the function returns its result to the caller.
- A function’s parameters and variables defined in its block are all local variables.

### Accessing a Function’s Docstring Via IPython’s Help Mechanism¶

- Following a function's name with `?` in IPython displays its docstring:

In [5]:

    square?

    Signature: square(number)
    Docstring: Calculate the square of number.
    File:      ~/Dropbox/books/2019/Python/PyCDS_JupyterSlides/ch04/<ipython-input-1-7d5dc51751d0>
    Type:      function

- If the function’s source code is accessible from IPython, `??` displays the function’s docstring and full source-code definition:

In [6]:

    square??

    Signature: square(number)
    Source:
    def square(number):
        """Calculate the square of number."""
        return number ** 2
    File:      ~/Dropbox/books/2019/Python/PyCDS_JupyterSlides/ch04/<ipython-input-1-7d5dc51751d0>
    Type:      function

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 4 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.