How to define a function in Python?



A function in Python is a collection of connected statements that carry out a certain activity. Functions assist in segmenting our program into manageable, modular portions. Our program becomes more organized and controlled as it gets bigger and bigger. It also makes the code reusable and prevents repetition. A function is defined using the keyword def following a space and the function_name with parentheses and a colon. The next line contain a indented code block to do something

Method is a function that is associated with an object. In Python, method is not unique to class instances. Any object type can have methods.

It is said that everything in Python is an object. In python, functions too are objects. So they have attributes like other objects. We can also assign new attributes to them, as well as retrieve the values of those attributes. Functions can have even functions written inside them.

Syntax

Below is the syntax to create a function in python.

def function_name(parameters):
   """docstring"""
   statement(s)

The function definition that is displayed above consists of the following elements.

  • It begins with the keyword def in the function header. A name for the function that serves as a unique identifier. The conventions for creating Python identifiers apply to naming functions as well.

  • The variables (arguments) that we use to supply values to a function. They are not required.

  • The function header ends with a colon (:).

  • Documentation string (docstring) that is optionally provided to explain the function's purpose.

  • The body of the function is composed of one or more legitimate Python statements. The indentation level for each statement must be the same (usually 4 spaces).

  • A return statement that is optional that allows the function to return a value.

Example 1

Creating a function can be done by using the code given below. Upon calling the function, the output generated is as follows.

def function(): print("Hello World") function()

Output

The output generated is shown below.

Hello World

Example 2

Once a function has been defined, it can be called from another function, a script, or even the Python prompt. We just enter the function name and the necessary parameters to call the function.

Let’s look at an example of creating a function and calling it.

def mutiply(a, b, c): res = a*b*c; print("Result is:",res) mutiply(24,556,993)

Output

The output is shown in the line below.

Result is: 13250592

The return statements

In order to leave a function and return to where it was called, utilize the return statement.

Syntax

The syntax for a return expression is given below.

return [expression_list]

The value is returned once the expression in this statement has been evaluated. The function will return the None object if there is no expression in the statement or if the return statement itself is missing from a function.

Example 

In this example, we will see how to define and use a function that returns certain values.

def cube(x): r=x**3 return r print(cube(5))

Output

The output produced and returned by the cube function above can be printed using a print statement and the output obtained is shown below.

125

Advertisements