14
The Weaponization of Functions
"The Art of Functions" covered the basics of functions but we need to learn quite a few important utilities as we start using various libraries or write our own code. Today, we will dive into a few major concepts as we learn more about *args, **kwargs, Higher-order functions, and recursion.
These allow unlimited positional arguments to be passed inside a function with input or output functions. *args are tuples and can be looped through.
*args are usually used when you'd like your user to input many arguments and make them work in a similar kind of way. To put it in easier terms, do one action with the arguments.
For example, in the above image we only sum up the arguments. You can similarly set colors for an image with various hex codes as arguments.
These allow unlimited keyword arguments to be passed inside a function with input or output functions. **kwargs are dictionaries and can be used as a key, value pair.
**kwargs are usually used when you'd like your user to input many arguments but to make them work in various different ways. To put it in easier terms, do multiple actions with the arguments.
For example, in the above image we sum and multiply the arguments. You can similarly set various properties such as the width, the height of an image with keyword arguments. These are extremely popular in Data Science libraries such as Seaborn and Matplotlib.
Higher-order functions are usually used when you'd like your user's choice to be separate and divide the actions.
For example, in the above image we use the cal
function to take another function add
or mul
and pass in the argument given by the user to return a value. These are generally used if you are using a GUI application to assign respective functions to a button or a label.
- Higher-order function takes other functions as parameters
- Remember to not add parenthesis inside the argument
Recursion is usually used when you'd like to loop in a particular set of actions until another set of actions is done. Recursion is extremely handy as they can work with multiple libraries without necessarily using the while-loop. Since Recursion is an important topic we will discuss more, while understanding Data Structures especially Binary Trees.
Functions are generally used to avoid repeatable code which not only makes our code look ugly and extremely hard to debug but also helps with readability since they help summarize a particular set of work inside a defined function. In general, functions with outputs are widely used and are more popular as they serve greater flexibility towards clean code.
This is only a brief overview of the power functions hold to make our life easier. We will dive more into how we can use them and learn more on where we can use them in the upcoming series understanding the nitty-gritty stuff.
If you have any interesting suggestions or feedback, feel free to connect with me on Twitter. I also have a newsletter, which I send out every week Thursday. You can subscribe it here.
Hope you enjoy this, see you next week! đđŒ
14