Functions in Python

Posted by

In Python, a function is a block of code that performs a specific task and returns a result. Functions allow you to reuse and modularisation your code, making it easier to write and maintain.

Here is an example of a simple function in Python:

In this example, the function greet takes a single parameter name and prints a greeting to the screen. To call the function, you simply need to use its name followed by a set of parentheses and any required parameters.

Return values

In addition to performing a task, functions can also return a value to the caller. To return a value from a function, you can use the return statement. For example:

In this example, the function add takes two parameters a and b , adds them together, and returns the result to the caller. The return value can then be assigned to a variable, as shown in the example.

Optional parameters

You can also define function parameters as optional, which means that they do not have to be passed when the function is called. To define an optional parameter, you can give it a default value in the function definition. For example:

In this example, the parameter greeting is optional and has a default value of “Hello”. When the function is called without the greeting parameter, it will use the default value. If the greeting parameter is passed, it will use the value provided.

Variable number of parameters

In Python, you can define a function to accept a variable number of parameters using the *args and **kwargs syntax.

  • *args allows you to pass a variable number of positional arguments to a function. The arguments are passed as a tuple. For example:

    In this example, the function sum_numbers takes a variable number of positional arguments and sums them up. The arguments are passed as a tuple and can be accessed inside the function using a loop or by indexing the tuple.

    • **kwargs allows you to pass a variable number of keyword arguments to a function. The arguments are passed as a dictionary. For example:

    In this example, the function greet takes a variable number of keyword arguments and prints a greeting to the screen. The arguments are passed as a dictionary and can be accessed inside the function using the dictionary syntax.

    Anonymous functions (lambda functions)

    In Python, you can also create anonymous functions (also known as lambda functions) using the lambda keyword. Lambda functions are small, single-line functions that are usually used as arguments to other functions. Here is an example:

    In this example, the function square is defined both as a regular function and as a lambda function. The lambda function version is a single line of code and does not have a name.

    Lambda functions are useful when you need to pass a simple function as an argument to another function. For example:

    In this example, the function square_list is defined both as a regular function and as a lambda function. The lambda function version is a single line of code and does not have a name.

Leave a Reply

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