Python Functions

Python Functions, What is Function?, Types of Python Functions, Create Python Functions, Calling a Python Function, and Functions Advantages.

Python Functions

1. What is Function?
2. Types of Functions in Python
3. Advantages of Functions
4. User-Defined Functions
5. Python Function with return a value
6. Python Function with returns nothing
7. Python Functions with Arguments
8. Python Function with default parameters

1. What is Function?

A function is a block of code that only runs when it is called. We can pass data, known as parameters, into a function, and a function can return data as a result.

A function can be called multiple times to provide reusability and modularity to the Python program.

2. Types of functions in Python

There are mainly two types of functions in Python.

1. User-defined functions – The user-defined functions are those defined by the user to perform the specific task.

2. Built-in functions – The built-in functions are those functions that are pre-defined in Python.

3. Advantage of Functions in Python
  • Reusability is the main achievement of Python functions.
  • Using functions, we can avoid rewriting the same code again and again in a program.
  • We can call Python functions multiple times in a program and anywhere in a program.
  • We can track a large Python program easily when it is divided into multiple functions.
  • Easier code maintenance.
4. Python User Defined Functions

A function is a reusable block of programming statements designed to perform a certain task. To define a function, Python provides the def keyword.

i. Function with no arguments and return a value
ii. Function with arguments and return a value
iii. Function with no arguments and returns nothing
iv. Function with arguments and returns nothing

A function is a reusable block of programming statements designed to perform a certain task. To define a function, Python provides the def keyword.

5. Python Function with return a value

Create a Function (Syntax):

def function_name(parameters):
“””docstring”””
statement1
statement2


return statement

Guidelines for writing function in Python

  • The def keyword, along with the function name is used to define the function.
  • The identifier rule must follow the function name.
  • A function accepts the parameters (arguments), and they can be optional.
  • The function block is started with the colon (:), and block statements must be at the same indentation.
  • The return statement is used to return the value. A function can have only one return

Create a Function (Example):

def sum():
a = 10
b = 20
c = a+b
return c

# calling sum() function (To call a function, use the function name followed by parenthesis:)
sum()

# calling sum() function in print statement
print(“The sum is:”,sum())

Note: The return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function.

6. Python Function with returns nothing

 Creating function without return statement

# Defining function
def sum():
a = 10
b = 20
c = a+b

# calling sum() function in print statement
print(sum())

Output: None

7. Python Functions with Arguments

The arguments are types of information that can be passed into the function. The arguments are specified in the parentheses. We can pass any number of arguments, but they must be separated with a comma.

Example 1

#defining the function
def func (city):
print(“My City is: “, city)

#calling the function
func(“Hyderabad”)

Example 2:

#Python function to calculate the sum of two variables

#defining the function
def sum (x,y):
return x+y;

#taking values from the user
a = int(input(“Enter x: “))
b = int(input(“Enter y: “))

#printing the sum of x and y
print(“Sum = “,sum(x,y))

8. Python Function with default parameters

The following example shows how to use a default parameter value.

If we call the function without argument, it uses the default value:

Example:

def my_function(country = “Norway”):
print(“I am from ” + country)

my_function(“India”)
my_function()
my_function(“Canada”)

Note: The terms parameter and argument can be used for the same thing: information that is passed into a function.

A parameter is the variable listed inside the parentheses in the function definition. An argument is a value that is sent to the function when it is called.


Python Function Examples

1. Read an Integer and verify whether the value is a leap year or not?

# Read input for User

val = int(input(“Enter a Value: “))
#print (val)

# Create Function

def leap_year(val):
if ((val%4 == 0) or (val%400==0)):
print(“It is Leap Year”)
else:
print(“It is Not Leap Year”)

# Calling Function

leap_year(val)

2. Read four integers and find which one is the biggest number.

# Read input for User

val1 = int(input(“Enter a Value 1: “))
val2 = int(input(“Enter a Value 2: “))
val3 = int(input(“Enter a Value 3: “))
val4 = int(input(“Enter a Value 4: “))

# Create Function

def leap_year(val1, val2, val3, val4):
if ((val1>val2) and (val1>val3)and (val1>val4)):
print(“Value 1 is a Big Number”)
elif ((val2>val1) and (val2>val3)and (val2>val4)):
print(“Value 2 is a Big Number”)
elif ((val3>val1) and (val3>val2)and (val3>val4)):
print(“Value 3 is a Big Number”)
elif ((val4>val1) and (val4>val2)and (val4>val3)):
print(“Value 4 is a Big Number”)
else:
print(“Two or more Values are same”)

# Calling Function

leap_year(val1, val2, val3, val4)


Python Tutorial
Python Video
Follow me on social media: