Functions in Python

Functions in Python

Functions is Python are a block of statements which are executed only when they are called from some part of the program. Functions provide reusability of code as a set of statements needs to be written only once but can be executed multiple times with different inputs depending upon the program’s requirements. 

 

 

Types of Functions in Python

Function can be built-in or user-defined.

Built-in Functions in Python

Functions whose definitions are already provided in a module. One of the most commonly used built-in function is print() which is used to print some objects to a console.

User-defined Functions in Python

User-defined functions are those which are defined by the user using def keyword.

 

Create Functions in Python

Functions are created by using def keyword. This keyword is followed by the function name and paranthesis. Functions can also have parameter(s) which are supplied in the paranthesis. Paranthesis is followed by the function definition where you specify the lines of code you want your function to perform upon invocation.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#Function Definition
def function_name(parameter(s)):
statement(s)
#Function Call
function_name(parameter(s))
#Function Definition def function_name(parameter(s)): statement(s) #Function Call function_name(parameter(s))
#Function Definition
def function_name(parameter(s)):
    statement(s)

#Function Call
function_name(parameter(s))

Example: A function with no parameter

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python program to exemplify function creation with no parameters
# Function Definition
def studyExperts():
print("Welcome to StudyExperts!")
# Function call
studyExperts()
# Python program to exemplify function creation with no parameters # Function Definition def studyExperts(): print("Welcome to StudyExperts!") # Function call studyExperts()
# Python program to exemplify function creation with no parameters

# Function Definition
def studyExperts():
  print("Welcome to StudyExperts!")

# Function call
studyExperts()

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Welcome to StudyExperts!
Welcome to StudyExperts!
Welcome to StudyExperts!

 

 

Parameter

A parameter is a variable which is used to pass information received from the function call inside the function. It is not mandatory for a function to have parameter(s). Number of parameters can be any, depending upon the function’s requirements. Parameter(s) is(are) then used inside the function to achieve the desired result.

Example: A function with a parameter

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python program to exemplify function creation with a parameter
def MyFunction(name):
print("Welcome to the Python tutorials of StudyExperts! " + name +".")
MyFunction("Brandon")
MyFunction("Brenna")
MyFunction("Katia")
# Python program to exemplify function creation with a parameter def MyFunction(name): print("Welcome to the Python tutorials of StudyExperts! " + name +".") MyFunction("Brandon") MyFunction("Brenna") MyFunction("Katia")
# Python program to exemplify function creation with a parameter

def MyFunction(name):
  print("Welcome to the Python tutorials of StudyExperts! " + name +".")

MyFunction("Brandon")
MyFunction("Brenna")
MyFunction("Katia")

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Welcome to the Python tutorials of StudyExperts! Brandon.
Welcome to the Python tutorials of StudyExperts! Brenna.
Welcome to the Python tutorials of StudyExperts! Katia.
Welcome to the Python tutorials of StudyExperts! Brandon. Welcome to the Python tutorials of StudyExperts! Brenna. Welcome to the Python tutorials of StudyExperts! Katia.
Welcome to the Python tutorials of StudyExperts! Brandon.
Welcome to the Python tutorials of StudyExperts! Brenna.
Welcome to the Python tutorials of StudyExperts! Katia.

 

 

Default Parameter Value

A default or dummy value can be assigned to a parameter at the time of function creation so that if it is called without a parameter, it’s default value can be used inside the function.

Example: A function with a default parameter value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python program to exemplify function creation with a default parameter value
def MyFunction(name = "Bro"):
print("Welcome to the Python tutorials of StudyExperts! " + name +".")
MyFunction()
MyFunction("Brenna")
# Python program to exemplify function creation with a default parameter value def MyFunction(name = "Bro"): print("Welcome to the Python tutorials of StudyExperts! " + name +".") MyFunction() MyFunction("Brenna")
# Python program to exemplify function creation with a default parameter value

def MyFunction(name = "Bro"):
  print("Welcome to the Python tutorials of StudyExperts! " + name +".")

MyFunction()
MyFunction("Brenna")

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Welcome to the Python tutorials of StudyExperts! Bro.
Welcome to the Python tutorials of StudyExperts! Brenna.
Welcome to the Python tutorials of StudyExperts! Bro. Welcome to the Python tutorials of StudyExperts! Brenna.
Welcome to the Python tutorials of StudyExperts! Bro.
Welcome to the Python tutorials of StudyExperts! Brenna.

 

 

Function to Return Values

A function can be used to return values using return keyword.

Example: A function with a default parameter value that returns values

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python program to exemplify function creation with a default parameter value to return values
def MyFunction(number, mul = 2):
num = number * mul
return num
print(MyFunction(2, 7))
print(MyFunction(19))
print(MyFunction(23, 2))
# Python program to exemplify function creation with a default parameter value to return values def MyFunction(number, mul = 2): num = number * mul return num print(MyFunction(2, 7)) print(MyFunction(19)) print(MyFunction(23, 2))
# Python program to exemplify function creation with a default parameter value to return values

def MyFunction(number, mul = 2):
  num = number * mul
  return num

print(MyFunction(2, 7))
print(MyFunction(19))
print(MyFunction(23, 2))

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
14
38
46
14 38 46
14
38
46

 

 

Key Parameter

If a function has more than one parameter, then values of those parameters should be passed in order during function call. But, there is another way in which order is not required, you just imply have to pass the values in the syntax like key=value in the function call.

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python program to exemplify key parameter functionality
def MyFunction(var1, var2):
print(var1/var2)
MyFunction(var2 = 2, var1 = 12)
# Python program to exemplify key parameter functionality def MyFunction(var1, var2): print(var1/var2) MyFunction(var2 = 2, var1 = 12)
# Python program to exemplify key parameter functionality

def MyFunction(var1, var2):
  print(var1/var2)

MyFunction(var2 = 2, var1 = 12)

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
6.0
6.0
6.0

 

 

Unknown Number of Parameters

If you are not aware of the number of parameters a function will require, then you can simply prefix a parameter name with an asterisk which will make it an iterable. Values then passed to the iterable will be stored in it and can be accessed using indexing or looping over it.

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Python program to exemplify how to deal with unkown number of parameters
def MyFunction(*var):
result = 1;
for i in var:
result = result * i
return result
print(MyFunction(8, 3))
print(MyFunction(2, 4, 6))
# Python program to exemplify how to deal with unkown number of parameters def MyFunction(*var): result = 1; for i in var: result = result * i return result print(MyFunction(8, 3)) print(MyFunction(2, 4, 6))
# Python program to exemplify how to deal with unkown number of parameters
def MyFunction(*var):
  result = 1;
  for i in var:
    result = result * i
  return result

print(MyFunction(8, 3))
print(MyFunction(2, 4, 6))

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
24
48
24 48
24
48

 

Share this post

Leave a Reply

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