Python Function

by anupmaurya
245 views

In this article you will we learn about python function. How to create a function ,call a function ,passing arguments and more.

function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. It increases re-usability of code and removes redundancy.

Defining a Function

A function is defined using the def keyword in python. The basic syntax is

SYNTAX

def function_name(parameters):
              function body (statements)

The function body consist of indented statements. To end the function body, the indent is to be ended. Every time, a function is called the function body is executed. The parameters in the function definition are optional.

A function may have a return statement which returns a result. Once the return statement is executed in the function body the function is ended.

EXAMPLE

def square(x): return x*x;
Code language: Python (python)

Calling a function

The defined functions are needed to be called to execute the function body.

A function is called by its name by passing the required values as defined as parameters in the function definition.

SYNTAX

function_name(value)
#If we want to find the square of 9, then we can call the function like:
square(9)
#81

EXAMPLE

def greetings(): print("Hey there!") greeting() #Hey there! def demo_func(x): x=20 return x+30 demo_function(10) #50
Code language: Python (python)

Passing Parameters in a function by reference

Here, a reference of actual parameter is passed to the function.

The advantage of it is that whenever the parameter value is modified the change reflects back in the function call.

EXAMPLE

def demo(myList): print('Given List is:',myList) myList.append('ten') print('The list is changed to :',myList) demoList=['one','two','three'] print('The demo list before:',demoList) #The demo list before: one,two,three demo(demoList) #Give List is: one,two,three #The list changed to :one.two,three,ten
Code language: Python (python)

Note: In Python, scalar values are passed by-value. Lists and other objects are passed by reference.

Arguments

The values passed in the parameters of a function are called arguments. The arguments provided in the function definition should match while calling the function. Or else, it will result in error.

EXAMPLE:

def demo(myList): print('Given List is:',myList) myList.append('ten') print('The list is changed to :',myList) demoList=['one','two','three'] print('The demo list before:',demoList) #The demo list before: one,two,three demo(demoList) #Give List is: one,two,three #The list changed to :one.two,three,ten
Code language: PHP (php)

Default Arguments:

Function arguments can be provided with default values in function definition.

EXAMPLE:

def greetings(name,greeting='Good Morning'): print(greeting+ ' '+name+ ' !') greeting('Mansi') Good Morning Mansi!
Code language: PHP (php)

Here name does not have a default value. So it is going to take the value that is provided during the function call. But greeting has a default value and hence not needed to be provided with any value while calling the function.

Note: If a function has default arguments, all the arguments to its right are to provided as default arguments i.e. no non-default arguments can be present once a default argument is provided.

Types of Functions

Basically, we can divide functions into the following two types:

  1. Built-in Functions – Functions that are built into Python.
  2. User defined Functions- Functions defined by the users themselves.

You may also like