Functions in Python

Functions are group of statement created to perform specific task. There are several built in functions in Python such as print() to display output, sqrt() for calculating square root.

Programmers can create their own function which are known as user defined function.

Advantages of function

  • They are important part of programming language
  • Once function is written it can be called or reuse multiple times
  • Function are reusable code
  • Maintenance of code becomes easy with the help of function
  • The use of function reduce the length of the program

Difference between function and Method

  • Functions are reusable code and are created to perform specific task
  • Function can be called using its name
  • Function is called method when it is written inside a class
  • Only difference between function and a method is their placement and the way they are called
  • Method is called using objectname or classname
    • objectname.methodname()
    • classname.methodname()

Defining a function

  • Function starts with def keyword followed by function name.
  • After the function name write parentheses() which may contain parameters.
  • At the end of the line write colon :
  • Syntax
def functioname(par1, par2...parn):
    *** Function Docstring ***
    Statements
  • The first statement of the function is the docstring
  • They are enclosed within triple double quotes
  • The main purpose of a docstring is for documentation and they are optional
  • Example
def add(a,b):
    """ Adding two values """
    c = a + b
    print(c)
  • Function starts with def keyword followed by function name
  • add is the name of the function
  • Above function includes two parameters in parentheses a and b
  • Parameters a and b receives values from outside of the function
  • Function body contain three statements
    • First statement is the docstring
    • Second statement is c = a + b
    • Third statement is the print statement print(c)

Calling a function

  • Function can be called using its name
  • While calling a function, we should pass necessary parameters in the parentheses as : add(10,22)
  • Example
def add(a,b):
    """ Adding two values"""
    c = a + b
    print(c)

add(10,22)

Output

32
  • In the above code, we have created a function add which includes two parameters(a, b)
  • Parameters a and b getting their values from outside of the function
  • We using function name to call a function. In the above code using function name add(10.22) and providing two values. These two values are assign to parameters a and b, a = 10 and b =22
  • Values 10 and 22 are knows as arguments
  • The same function can be called multiple times. Below is the example
def add(a,b):
    """ Adding two values"""
    c = a + b
    print(c)


add(10,22)
add(30,20)
add(100,200)

Output

32
50
300

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s