Python Functions Tutorial
Introduction of Functions in Python
A function is a logical block of code that does a specific task. Optionally, a function takes one or more arguments, and can return a value.
A function can also be defined as something that transforms given arguments.
Syntax
def
keyword is used to define a function in Python. A function can have zero, one, or more parameters. The code inside a function must have indentation.
def functionName(parameters):
statement(s)
where
def | Python keyword to define a function. |
functionName | Name given to the function, using which we can call it in the program. |
parameters | [Optional] Input to the function. |
statement(s) | Python code. |
A function can return a value. Use return
statement to return something from this function.
Example
The following is the definition of a function with name add
, that takes three numbers as arguments, adds them, and returns the sum.
def add(a, b, c):
sum = a + b + c
return sum
Call the Function
To call the function, use the function name, followed by parenthesis, and pass arguments if the function accepts any. For example, add(4, 8, 2)
.
Now, let us use the above add
function in the following program, where we define this function, call the function by passing three integer values, store the returned value by the function, and print the result.
Python Program
def add(a, b, c):
sum = a + b + c
return sum
#calling the function
result = add(4, 8, 2)
print('Result :', result)
Output
Result : 14
Function with No Parameters
As already mentioned, parameters are optional for a function. For example, if we would like to have a function that just prints 'Hello World'
to console output, then the function does not need any parameters.
In the following code, we shall define a function with no parameters, and just prints a string constant to the console.
def printHelloWorld():
print('Hello World')
Default Value for Parameters
We can provide a default value for parameter(s) when defining a function. Just assign the parameter with the default value using assignment operator =
.
In the following program, we define a function add() with three parameters, where the default value for the third parameter is set to 0
.
Python Program
def add(a, b, c = 0):
sum = a + b + c
return sum
print('Result :', add(2, 3))
print('Result :', add(2, 3, 6))
Output
Result : 5
Result : 11
When add(2, 3)
is called, value for the parameter c
is not passed. Therefore, default value of 0
considered. And when add(2, 3, 6)
is called c
is set to 6
.
Function with Return Statement
To return a value from a function, use return statement. The return statement contains the return
keyword followed by the value to return.
In the following code, we shall define a function that takes a number n
for its parameter, and returns n*n + 5
.
def someFunc(n):
result = n * n + 5
return result
Function with Arbitrary Arguments
We can also define a function that can take any number of arguments. Place the symbol *
before the parameter name, and this parameter can take any number of arguments.
In the following example, we define an add
function, that can take any number of numbers as arguments, and return their sum.
Python Program
def add(*args):
result = 0
for x in args:
result += x
return result
print('Sum :', add(4, 5))
print('Sum :', add(4, 5, 7))
print('Sum :', add(4, 5, 7, 1))
Output
Sum : 9
Sum : 16
Sum : 17
Summary
In this tutorial of Python Examples, we learned about Functions: how to define them, what are parameters, how to pass arguments in the function call for parameters, how to returns a value from a function, etc.