Thursday, December 26, 2024
Google search engine
HomeLanguagesFunctions in Python with Example

Functions in Python with Example

In this post, you will learn everything about functions in python programming like, what is a function in python programming language., How many types of functions in python, how to create the function and how to call functions in python.

Before we move ahead in python programming, you must better understanding functions in python.

What is function in python

In Python programming, a function is a function with a set of statements written inside it. Which is used to perform a specific task?

For example:- Like you have to repeatedly check-in your program whether this number is even or odd. So for this, you can create a function and call this function again and again.

Types of functions in python

There are basically 2 types of functions in Python. One that creates a user. And other functions that are built-in Python. The function created by the user is called the user-defined function and the function provided by Python is called the build-in function.

  • User define function in python
  • Predefine function in python

User define the function in python

These are functions that the user creates according to their requirement. This is called a user defined function

Syntax of Function

def function_name(parameters):
"""docstring"""
//set of statement(s)

Following are the rules for creating a function in python

When you create a function in the function python, you have to take care of the following things. Which is given below:

  1. You have to write the def keyword before you write the function name.
  2. The name of the function should be unique. more info click me
  3. As soon as the bracket of the function is closed, you will have to write colon (:).
  4. Parameters (arguments) through which we pass values to a function. They are optional.
  5. Optional return statement to return a value from the function.

Create a function in python

Let’s create the first function in python:

def evenOddFunc( x ): 
    if (x % 2 == 0): 
        print "even"
    else: 
        print "odd"

We have created the above function. It will then check whether the number passed in it is even or odd number.

Note: When you call this function, you have to pass a number inside it as a function parameter.

Call the function in python

You will know how to call the created function in python.

We have created the function of checking the even and odd numbers. Now we will call this function with parameters.

evenOddFunc(2) 
evenOddFunc(3) 

Output

even
odd

Note:- Parameters and arguments are the same things in python functions. basically is used to pass values ​​inside its function. Inside a function, you can pass n number of arguments and parameters.

Passing a list as an argument to a function python

Another important thing about python function, you can pass (string, number, list, dictionary etc.) as argument/parameter inside python function.

Let’s take an example, inside a function, which we will pass the list of argument

def my_num(num):
  for x in num:
    print(x)

nList = [1, 2, 3]

my_num(nList)

Output

1
2
3

Predefine function in python

Pre Defined / in-build function in Python, these are the functions, which are already made in Python. The user only uses these functions according to his requirements.

Some of the daily useful Pre-built python function name are print(), abs(), bool(), chr(), any(), help(), hash() etc

RELATED ARTICLES

Most Popular

Recent Comments