Functions in Python

Whenever there is a need to perform certain actions under certain conditions it is better to define that piece of code in such a way that it can be reused as and when there is a need rather than repeating the same code again and again. This can be achieved with the help of a function. In real world you will not find any application being created without functions.

Python has a set of in built functions which are very helpful. We have come across many such in-built functions while working with strings.

How to define a function

To define a function Python has defined a keyword ‘def’ which is followed by the name of the function and parantheses -> () followed by a colon -> ‘:’, as you will soon see in the function syntax. If the function requires any arguments then it is passed through these parentheses or else they are left blank. If the job of a function is to return a value at the end then the last statement will be a ‘return’ statement. Keeping this information in mind have a look at the following syntax:

def my_function(args):
    perform_these_steps
    return value

To get a better understanding have a look at the following piece of code

def hello(name):
    print("Hello " + name + "!!")

val1 = input("What's your name? : ")
hello(val1)

Download the code Run the code

1. def hello(name): – defines the function. ‘def ’ is the keyword to define the function. ‘hello’ is the name of the function. ‘name’ is the argument that is passed to the function.
2. When you execute the code you will be prompted to enter a name. The function input(“What’s your name? : “) will prompt the user. Input() method is used when you want the input from the user. Whatever value you enter will be assigned to the variable val1.
3. When you say hello(val1), the function hello will be invoked. The value of val1 will be assigned to the argument ‘name’ in the function definition.
4. print(“Hello ” + name + “!!”) in this statement the function will generate a Hello string. Run the code and see for yourself.

Passing default values as arguments

Can you guess the outcome of the following code? And why?

def hello(name = "World"):
    print("Hello " +  name + "!!")

val1 = input("What's your name? : ")
hello()

Download the code Run the code

1. Look at the function definition – def hello(name = “World”):. Here, the argument name= “World” signifies that if no value is passed to the function then by default it will take the value “World” as argument.
2. So, at the end of the code when a call is made to the function as hello() then, irrespective of what value you have entered when prompted, the function will print “Hello World !!” as the value that you provided was not passed on to the function as argument. Therefore, the default value was used.

So, can you now tell why the following code gives an error when executed?

def hello(name):
    print("Hello " + name + "!!")

val1 = input("What's your name? : ")
hello()

Download the code Run the code

1. Look at the function definition – def hello(name). Here, the argument ‘name’ has no default value.
2. So, it is mandatory to pass an argument to the function.
3. If you make a call to the function with “hello()”, then an error will be generated.

Function returning a value

Have a look at the following code. It shows how a value is returned from a function. We have defined a function that takes to numbers, adds them and returns the value.

def addition(var1=0, var2=0):
    s = int(var1) + int(var2)
    return s

val1 = input("Enter first number : ")
val2 = input("Enter first number : ")
print("sum of " + str(val1) + " and " + str(val2) + " = " + str(addition(val1,val2)) )

Download the code Run the code

<<< Loops in PythonPython Classes, Objects and OOP concepts >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .