Time for action – defining functions

Let's define the following simple function:

  1. Print Hello and a given name in the following way:
    >>> def print_hello(name):
    ...     print('Hello ' + name)
    ...
    

    Call the function as follows:

    >>> print_hello('Ivan')
    Hello Ivan
    
  2. Some functions do not have arguments, or the arguments have default values. Give the function a default argument value as follows:
    >>> def print_hello(name='Ivan'):
    ...     print('Hello ' + name)
    ...
    >>> print_hello()
    Hello Ivan
    
  3. Usually, we want to return a value. Define a function, which doubles input values as follows:
    >>> def double(number):
    ...     return 2 * number
    ...
    >>> double(3)
    6
    

What just happened?

We learned how to define functions. Functions can have default argument values and return values. ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.