Python Functions

Functions in Python

Python Functions are the most important aspect of an application. A function can be defined as the organized block of reusable code, which can be called whenever required.

Python allows us to divide a large program into the basic building blocks known as a function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the Python program.

The Function helps to programmer to break the program into the smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized.


There are mainly two types of functions.

  • User-define functions – The user-defined functions are those define by the user to perform the specific task.
  • Built-in functions – The built-in functions are those functions that are pre-defined in Python.

Advantage of Functions in Python

There are the following advantages of Python functions.

  • Using functions, we can avoid rewriting the same logic/code again and again in a program.
  • We can call Python functions multiple times in a program and anywhere in a program.
  • We can track a large Python program easily when it is divided into multiple functions.
  • Reusability is the main achievement of Python functions.
  • However, Function calling is always overhead in a Python program.

Creating a Function

Python provides the def keyword to define the function. The syntax of

Syntax:

def my_function(parameters):  
      function_block  
return expression  

Function Calling

In Python, after the function is created, we can call it from another function. A function must be defined before the function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by the parentheses.

Example:

#function definition  
def hello_world():    
    print("hello world")    
# function calling  
hello_world()      

Output:

hello world

The return statement

The return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function.

Syntax

return [expression_list]  

Example


# Defining function  
def sum():  
    a = 10  
    b = 20  
    c = a+b  
    return c  
# calling sum() function in print statement  
print("The sum is:",sum())  

Output:

The sum is: 30

Keyword arguments

Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.

This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. You can also make keyword calls to the printme() function in the following ways −

#!/usr/bin/python

# Function definition is here
def printme( str ):
   "This prints a passed string into this function"
   print str
   return;

# Now you can call printme function
printme( str = "My string")

Output:

My string

Default arguments

A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.

Example:

#!/usr/bin/python

# Function definition is here
def printinfo( name, age = 35 ):
   "This prints a passed info into this function"
   print "Name: ", name
   print "Age ", age
   return;

# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )

Output:

Name:  miki
Age  50
Name:  miki
Age  35

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *