Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

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.