Python Functions

In Python, a function is a block of statements that performs a particular task. The main idea is to group tasks that we often do repeatedly into a single function. This way we can simply call the function whenever needed and reuse the code efficiently without writing the same code multiple times for different inputs.

Advantages of Using Functions in Python

The following are some of the key benefits of using Python Functions

  • Functions help in the reusability of code.
  • It also reduces the code length.
  • It improves the readability of code.

Declaration of Python Function

The declaration of the function in Python is done in the following way:

Python Functions

Types of Functions in Python

The following are the types of functions used in Python:

  • Built-in Functions: Python standard functions that can be used anytime.
  • User-defined Functions: Functions created by the users on the basis of their requirements.

Creating a Function in Python

In Python, a function is defined using the def keyword. Any type of functionalities and properties as per our need can be added to the functions.

Here is an example showing how to write a function in Python:

# Simple example to write a function  
  
# using def keyword to define a function  
def welcome():  
  print("Hello, Users! Welcome to Learn Python App!!")  

Explanation:

In this example, we have used the def keyword to define a function. Here, the function name is welcome(). Inside this function, we have added a block of code to print a welcome message for the users.

Calling a Function in Python

Once we create the function in Python, we can call it by using the name of that function followed by a parenthesis. This parenthesis encloses the arguments, if any, of that specified function.

Here is an example showing how to call a Python function:

Example

# Simple example to write a function  
  
# using def keyword to define a function  
def welcome():  
  print("Hello, Users! Welcome to Learn Python App!!")  
  
# calling of the defined function  
welcome()  

Output:

Hello, Users! Welcome to Learn Python App!!

Explanation:

In this example, we called the defined function welcome() by using the name of the function followed by parenthesis. We have not specified any arguments, as the function itself does not require any parameters in this case.

Python Function Arguments

Function Arguments in Python are the values passed into a function when it is called. These values are used by the function in order to perform its operations.

Here is an example of Python Function Arguments:

Example

# Simple example to write a function  
  
# using def keyword to define a function  
def welcome(name): # <--- parameter  
  print(f"Hello, {name}! Welcome to Learn Python App!!")  
  
# calling of the defined function  
welcome("John") # <--- argument  

Output:

Hello, John! Welcome to Learn Python App!!

Explanation:

In this example, we defined a function welcome(name), where ‘name’ is the string parameter enclosed within the parenthesis. Inside this function, we printed a welcome statement. We then called the function by passing an argument to it.

Let us see another example:

Example

# Simple Python Program to add two numbers  
  
# defining a function to add two numbers  
def addition(num1, num2): # <- 2 parameters (num1, num2)  
  # storing the sum of the numbers  
  sum = num1 + num2  
  # printing the result  
  print(num1, "+", num2, "=", sum)  
  
# calling the function  
addition(18, 15)  # <- passing 18 and 15 as arguments to the function  

Output:

18 + 15 = 33

Explanation:

In the above example, we have defined a function addition() having two parameters – num1 and num2. Inside this function, we stored the sum of the numbers and printed it.

Outside the function, we called the function by passing the values – 18 and 15 as arguments.

Types of Python Function Arguments

There are several types of function arguments:

  • Positional Arguments
  • Keyword Arguments
  • Default Arguments
  • Arbitrary Arguments (Variable-length arguments *args and **kwargs)

Python Return Statement

In Python, the return statement is used in order to return a value from the function.

Let us take a look at the following example:

Example

# defining a function  
def squareOf(num):  
  res = num ** 2  
  return res  # using return statement  
  
# function call  
sqr = squareOf(13)  
  
# printing the result  
print("Square of 13:", sqr)  

Output:

Square of 13: 169

Explanation:

In the above example, we created a function called squareOf(). This function accepts a number as an argument and returns the square of that number.

Therefore, when we pass 13 to the function, it returns its square, i.e. 169. We store this returned value in a variable and print it for display.

Note: The return statement also indicates that the function has ended. Any code written after the return statement does not execute.

Python Pass Statement

The pass statement acts as a placeholder for future code. This way, we can prevent errors from empty code blocks. It is generally used where the code is planned but has yet to be written.

Let us see an example:

Example

# defining a function  
def function_name():  
  pass  # using pass as placeholder  
  
# calling the function  
function_name()  

Explanation:

In this example, we have used the pass statement in the function. This pass statement, here, acts as the placeholder for the future code block. If we call the function, it will not return any error.

Conclusion

Functions are the building blocks of any programming language. They are used in Python to build clean, reusable, and efficient code. Whether you want to use the inbuilt functions or create your customized functions, they allow developers to organize the logic into a single block, making the code readable, user-friendly, and easier to debug or extend. From simple parameter handling to advanced concepts like recursion, anonymous (lambda) functions, nested functions, and variable-length arguments (*args and **kwargs), functions are essential to writing scalable and maintainable Python programs. Having command over Python functions will help you to enhance your problem-solving skills.

Comments

Leave a Reply

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