Function() in Python-Complete Tutorial

function in python

This Function() in Python-Complete Tutorial is made for you to give complete knowledge of Function() in Python. At the end of this tutorial, you will have full knowledge of Function() in Python.

Hello, & Welcome! to the Function() in Python-Complete Tutorial.

Part 9- Function() in Python-Complete Tutorial

In this article of Python Tutorial, you will learn following-

  1. What is Function() in Python?
  2. Syntax of Function().
  3. How to call a Function?
  4. How to pass parameters in Function()?
  5. Return in Python.
  6. Some Examples using Function().

What is Function() in Python?

A Function is consists of a number of statements that take some input to perform some calculation and generate output. The Function makes your code more manageable.

You can create a function only one time and use it multiple times. It saves your time.

Suppose you are writing code and you created a function for some task, but in future, you need to perform the same task again, so instead of writing the full code again, you can simply call this function.

A Function makes code reusable and manageable.

Syntax of Function()

Syntax of Function() –

def function_name(parameters):
    statement 
  • For defining any function in python, there is a keyword ‘def’, which means ‘define’.
  • After using the ‘def’ keyword we use, function name. Inside the function name, we pass parameters.

Let’s have a look at the example of a function()-

def greet():
   print("Hello World")
greet()
  • In function, indentation is very important for the function body.
  • A colon(:) is compulsory at the end of the function.

How to call a Function?

After defining a function you can call the function anywhere in the program by just writing the function name with the parameters.

Let’s have a look at the example-

>>>greet('Alex')

How to pass parameters in Function()?

You can pass multiple parameters in the function. First, have a look at one example-

def greet(name,time):
   print(f'Good{time}{name},hope you are good')
greet('Alex','morning')

OUTPUT-
Good morning Alex,hope you are good.
  • Here, in greet(name, time), name and time are variable or parameters we are passing through function, which we can access it in function later.

You can use user input, which means a user can enter their name and time. Let’s see in the example.

def greet(name,time)                               #function define
   print(f'Good{time}{name},hope you are good')    #Function Body
name=input('enter your name:')                     #user input
time=input('enter greet time:')                    #user input
greet(name,time)                                   #Function calling

OUTPUT-
enter your name: Alex
enter greet time: Morning
Good Morning Alex, hope you are good.
  • Here, at the time of function calling greet(name, time), we are passing name and time as a parameter, but if we don’t pass anything here, means we left if blank like-greet(), so error comes. Therefore, we must pass the variable at the time of function call.
  • To avoid this error, there is one alternate method, you can pass a value in the variable at the time of function definition.
  • Let’s understand it with the help of an example.
def greet(name='Alex',time='Morning')              #function definition.
   print(f'Good{time}{name},hope you are good')    #Function Body
greet()                                            #Function calling

OUTPUT-
Good Morning Alex, hope you are good.
  • Here, in the example, as you see, we pass values at the time of a function definition, and then we call a function with 0 parameters. By doing this, you will not get an error.
  • One thing should be kept in mind that if you are using user input as we did in the previous example, then you have to pass values at the time of a function call.

Parameter Overriding-

Overriding means put a new value on the previous one. This case happen in python too. This happened when you pass a value at the time of function definition and again you pass another value at the time of function call. Then the new value passed at the time of function call override the previous value of function definition.

Let’s understand it with the help of an example-

def greet(name='Alex',time='Morning')              #function definition.
   print(f'Good{time}{name},hope you are good')    #Function Body
greet(name='John')                                 #Function calling

OUTPUT-
Good Morning John, hope you are good.
  • Here, in the example, as you see Output is ‘Good Morning John, hope you are good’, here name is ‘John’ not an ‘Alex’, because ‘John’ overrides the previous value ‘Alex’.

Return in Python.

Return statements allows the function to return a value. After the return statements, no other statement is executed. Return function is used only inside the function.

Syntax of Return Statement-

def function_name():
    statements
    return [value]

Let’s understand Return statement with the help of an example-

def multiplication(i):
   return 2*i
print(multiplication(2))
print(multiplication(3))
print(multiplication(4))

OUTPUT-
4
6
8
  • Here, first value of i is 2 and it is multiplied by 2 that is 4 and the return statement is returning a value 4.

Some Examples using Function().

Here, we will discuss some basic programs, which are made using a function. So, you get a clear idea of function.

Let’s start with our first program that is a program to calculate the area of Circle.

Example 1- Program to Calculate an Area of Circle-

def area(radius):
   print(3.142*radius*radius)
radius=int(input('enter a radius of circle-'))
area(radius)

OUTPUT-
enter a radius of circle- 5
78.55
  • Here, first, we passed ‘radius’ as a parameter in a function definition, then we have written the formula of area of a circle in print. After that, we asked the user to enter the radius of the circle by using input. Then we called the function ‘area’ and inside ‘area’ function we passed the ‘radius’ parameter.

Example 2- Program to Calculate Volume of Cylinder-

The formula of cylinder volume= area of circle * length of a cylinder

Here, we need to calculate area of a circle, then use the area of a circle to find the volume of a cylinder.

def area(radius):
    return 3.142*radius*radius    #we are returning value of area, and store it later
def vol(area,length):
    print(area*length)
radius=int(input('enter a radius of circle-'))
length=int(input('enter a length of cylinder-'))
area_calc=area(radius)           #we are storing the return value of area function.
vol(area_calc,length)

OUTPUT-
enter a radius of circle- 5
enter a length of cylinder-6
30
  • Here, we are storing the return value of area function in the variable named area_calc. But, if you don’t want to store the return value in any variable, then you can directly pass the function inside the function. Like that-

vol(area(radius), length)

  • Here, area(radius) is also a function which we are passing inside another function vol.

That’s all for Function() in Python, I hope now you have a better understanding of Function() in Python.

congratulations! You successfully learned Function() in Python.

Range() Function in Python-Complete Tutorial

In the next tutorial, we will start learning Variable Scope in Python

Till then, Enjoy Learning Python!

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

“Live as if you were to die tomorrow. Learn as if you were to live forever.” 

Mahatma Gandhi

Leave a Comment

Your email address will not be published. Required fields are marked *