The Many Face of Functions
An over-the-top view of the advanced usage of functions
Section 3: Advanced 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.
*args:
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.
**kwargs:
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:
Higher-order functions are functions that take in other functions as a parameter.
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.
Points to remember:
- Higher-order function takes other functions as parameters
- Remember to not add parenthesis inside the argument
Recursion:
Recursion is the method of calling a function within a function, similar to a while loop.
Recursion is usually used when you'd like to loop in a particular set of actions until another set of actions is done. While it's mandatory to have an ending condition (not shown in the image above) 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.
Conclusion
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! ๐๐ผ