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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset