Functions: Python Functions Demystified
Photo by RealToughCandy.com: https://www.pexels.com/photo/person-holding-an-orange-and-blue-python-sticker-11035474/

Functions: Python Functions Demystified

Functions are one of the key features of Python that allow developers to write reusable code and organize their programs into smaller, more manageable pieces.

In this article, we'll take a closer look at Python functions, their syntax, how to define and call them, and how to use arguments.

What are Python Functions?

A function is a block of reusable code that performs a specific task. By using functions, we can modularize and divide work into more manageable chunks. By writing functions, we can write code that is easier to read, test, and maintain.

If code is not organized into functions, we may have to repeat code in different areas of our program. This breaks one of the most important rules of programming: Don’t Repeat Yourself (DRY).

Defining a Function

The basic syntax for defining a function in Python is as follows:

No alt text provided for this image

Let's take a closer look at each part of the function above.

  • def: This keyword is used to define a function in Python. Every function will start with this keyword.
  • function_name: This is the name of the function that you are defining. You can choose any name for your function, as long as it follows the rules for variable names in Python (starts with a letter or underscore, no spaces, etc.). As best practice, your function names must have all lowercase letters, and if there is more than one word, the words must be separated by an underscore.
  • arguments: This is a comma-separated list of zero or more arguments that the function takes as input. Note that arguments are optional, and you can define a function with no arguments if you don't need any input.
  • function body: A function must have code to execute. This is where the code to be executed by the function goes. This part will always be indented. The function body can contain any valid Python code, including other function calls, loops, and conditional statements.
  • return: This keyword is used to return a value from the function. The return statement is optional, and if you don't include it in your function, the function will not return any value.

Here's an example of a function that takes three arguments, adds them together, and returns the result:

No alt text provided for this image

In this example, the name of the function is add_numbers. So, add_numbers() takes three arguments, x, y, and z, adds them together, and returns the results. Now note that x, y, and z (the variables inside the parenthesis) are called parameters. Parameters are placeholders for arguments. When we call this function, we will pass numbers for x, y, and z. These numbers are what we call arguments.

Calling a Function

Once you have defined a function in Python, you can call it from other parts of your program. To call a function, you simply use the name of the function and any arguments that it requires.

Here's an example of calling the add_numbers() function that we defined earlier:

Since the function has three (3) parameters, we have to pass three arguments when we call the function. Here is the complete code with the three numbers passed as arguments when we call the function.

No alt text provided for this image

In this example, we call the add_numbers() function with the values 3, 5 and 6 as arguments. The function adds these three values together and returns the result, which we store in the variable results. When we call the function, the return statement returns the value (14), which is stored in the variable "results."

Default Arguments to Functions

We can add default arguments to a function. Default arguments are arguments that will be used by the function when we don’t pass any arguments. In the code below, we have passed default arguments for parameters y and z. When we call the function and we do not pass any arguments for these parameters, it will use the default arguments of 10 and 15. Notice that since we only pass one argument for x, which is 3, the function uses default arguments for y and z, which is why we are getting an output of 28.


No alt text provided for this image

Python *args and **kwargs

There are times when we do not know how many parameters to pass into a function. In such circumstances, Python allows us to pass *args (non-keyword arguments) as a parameter. With *args passed as a parameter, we can pass as many arguments as we want. Using *args in our function makes our function more flexible. In the code below, we do not know how many numbers we need as arguments for our function, so we use *args. Because we have used *args, we can pass as many numbers as we want as arguments, and the function will still work. Note that you can use whatever word you want as an argument as long as you put an asterisk (*) at the beginning of the word. However, it is good practice to use the letters "args."


No alt text provided for this image

When you see **kwargs (keyword arguments) as a parameter, it means the function can accept any number of arguments as a dictionary (arguments must be in key-value pairs). Below, we are calculating the average of the numbers passed as arguments. Notice that we first pass two arguments (x = 6, z = 10), and then we pass three arguments (x = 6, y = 7, z = 10), and both times our function is able to calculate the average of the values.


No alt text provided for this image

Conclusion

Functions are a fundamental feature of Python. They enable programmers to create reusable and well-structured code. In this article, we covered the syntax of Python functions, how to define and call them, and provided some examples of functions in action. With these concepts in mind, you should be able to start using functions in your own Python code. Thank you for reading. Please share this story and subscribe to this newsletter if you are not yet a subscriber. You can also follow me on?LinkedIn. Join this Python group to interact with other Pythonistas.

...............................................................................................................................

Please go to?Amazon?and get this Python book if you are learning Python.

No alt text provided for this image
Sonu Yadav

Java Developer

1 年

I am interested

回复
Vígh Zoltán

Analytics Development Lead

1 年

Thanks for sharing, this is an awesome article!

回复
Geshin Aderemi

Web Developer | Backend Developer | Django Developer

2 年

Re Tangellapalli's observation, I'd suggest that you make a further clarification: Parameters are the variables provided in the function definition. Whereas arguments are values passed to the function when it's called. Cheers.

Sivasambhu Lenka

Assistant Manager - Enterprise Data Analytics & AI Strategy Ex-AON, Ex-Hewitt

2 年

Thanks for sharing

回复
Ebsa Umare

Access Network & Power Technician @ Ethiotelecom | Data Analyst [Excel | SQL | PowerBI] | Virtual Assistant

2 年

interested, course hint written!!

要查看或添加评论,请登录

Benjamin Bennett Alexander的更多文章

社区洞察

其他会员也浏览了