Loading...
Loading...
00:00:00

Functions In Python

A function in Python is a block of reusable code that performs a specific task. Functions help break down large and complex programs into smaller and manageable parts. In Python, you can create a function using the def keyword. Here's an example:

def greet(name):
  print("Hello, " + name + ". How are you today?")

greet("John")

There are two types of functions in Python:

  1. Built-in functions: These are the functions that are built into the Python language and are available to use in your code right out of the box. Some examples are print(), len(), str(), int(), etc.

  2. User-defined functions: These are functions that you create yourself to perform specific tasks in your code. The above example of the greet() function is a user-defined function.

In addition to these two types, there are also a few other special types of functions in Python, such as anonymous functions (lambda functions) and recursive functions.

In Python, functions can be classified into the following types based on their arguments:

  1. Required arguments: These are the basic type of arguments which are required to be passed while calling a function. The number of arguments passed to the function should match the number of required arguments defined in the function definition.

  2. Keyword arguments: These are the type of arguments where the parameter name and its value is specified. The advantage of using keyword arguments is that the order of the arguments can be changed as long as the names are specified.

  3. Default arguments: These are the type of arguments where a default value is assigned to the argument in the function definition. If the argument is not provided while calling the function, the default value will be used.

  4. Variable-length arguments: These are the type of arguments that allow a function to receive an arbitrary number of arguments. In Python, there are two types of variable-length arguments: a. *args: This syntax is used to pass a non-keyword variable length argument list to the function. It will be passed as a tuple. b. **kwargs: This syntax is used to pass keyworded, variable-length argument list to the function. It will be passed as a dictionary.

These are the main types of arguments in Python functions.

Create function 

In Python, a function is defined using the def keyword, followed by the function name and a pair of parentheses (). The arguments, if any, are placed within the parentheses. The function body is indented under the def statement and is executed when the function is called. The return statement is used to return a value from the function.

Here is the general syntax for defining a function in Python:

def function_name(arg1, arg2, ...):
    # Function body
    # Perform operations
    # ...
    return result

For example, the following function takes two arguments a and b and returns their sum:

def sum(a, b):
    return a + b

To call the function, you simply use the function name followed by the arguments in parentheses:

result = sum(10, 20)
print(result)  # 30

NOTE: It is important to call a function when you define. Only function defining is not give you output unless you call it.