Sunday, August 2, 2015

Python Basics - 011: Functions

Previous chapter: Lists and Tuples Next Chapter: Dynamic argument count



Defining Functions

Python functions are equivalent to Java Methods or C functions. If you look at documentation sometimes functions will be called as methods, It is because functions of an object are called methods conforming to Object oriented standards. Functions are defined before they are called in the code unlike in Java, because Python is interpreted and not compiled in advance. So python executes as soon as it gets the python file from top to bottom and throws an error when it encounters one. On the other hand Java wouldn't even consider executing a file with errors as error checks are done in compile time.

Syntax for creating a function is to use the def keyword. See example below to see a function in action


def printHi():
    print("Hi!")

printHi()

output:


Hi!

So whenever you want to say hi in the program you can call this function instead of using print() function. Although it doesn't make sense to use a function for a small task, more and more complex tasks need to be broken down into modules and each performing an operation which is delegated to a function. This just makes it easy to unit test the program and to make sure where the error is.

Functions can also accept parameters and return an object. While the number of parameters are limitless, there can be only one return value.


def printHiName(name):  # parameterized function, how about we say Hi using a name.
    print("Hi", name)

printHiName("Alan")

output of this will be


Hi Alan

Let's see another example for parameterised function with a return value


def add(var1, var2):  # function with a return value, how about adding 2 values?
    return var1+var2

print("Add 2 and 5: ", add(2, 5))

output of this will be


Add 2 and 5: 7

The same function can be called by using the name of the parameters to decrease confusion. This way of using the name of the parameter is called keyword argument:


add(var1=2, var2=5)

This will not make a difference in this method but using a large number of parameters you may get confused about what the parameters represent, this will increase readability and will enable us to change the order of the parameters like the example below:


add(var2=5, var1=2)

Default arguments while defining functions

Default arguments[2] are how we implement optional parameters for python functions. Optional or default arguments or parameters which have a default value if you don't use them.

See the example below:


def printHello(name="Stranger"):
    print("Hello ", name)

printHello("Alan")

printHello()

Here in the above example, both calls of printHello() are valid and when name is not passed, it defaults to "Stranger"

So the output will be:


Hello  Alan
Hello  Stranger

There is another way to implement multiple arguments for a function in Python, We'll see more about it in the next chapter.


P.S: all the code used in this tutorial is available in github


Reference:

  1. https://docs.python.org/3.4/tutorial/controlflow.html#defining-functions
  2. https://docs.python.org/3.4/tutorial/controlflow.html#default-argument-values



Previous chapter: Lists and Tuples Next Chapter: Dynamic argument count

No comments:

Post a Comment