Let me start with a fact: Every function returns a value when called.
Well, I don't know about you, but I was surprised to know about it in the first place!
Let's see an example to prove my point:
Output:
Welcome to my blog post
None
Did you see that? When I called the function, it has returned the special type None. The variable post stores it and prints it out in the second line.
Now, we can explicitly return a value from a function by using the return keyword.
Let's code a few examples:
1) Single return statement
Output:
21
As you can see in this example, we have given an expression which returns the product of given two numbers.
Output:
21
3.14
Try again!
Here, an explicit value is specified in multiple return statements.
3) return statement with multiple values
Output:
(21, 63, 210)
This example returns dynamic values in a tuple based on the given argument.
In general, the term parameters and arguments are being used interchangeably.
Although, with respect to a function:
It turns out that it's possible to define functions with various types of arguments in python. And there are three types of arguments/parameters, which can be combined.
The most useful type of argument is to specify a default value for one or more arguments, inside the parenthesis of a function definition. This creates a function that is flexible to use. Because this function can be called with fewer arguments than it is defined to allow.
Let's look at an example:
Output:
You have 120 minutes!
Let's watch a action type web series
You have 150 minutes!
Let's watch a thriller type web series
You have 200 minutes!
Let's watch a horror type movie
This function is called in several ways:
2) Keyword Arguments
Functions can also be called using keyword arguments of the form kwarg=value.
For instance, consider the above example of popcorn_time, function which accepts one required argument(time) and two optional arguments(genre, watch). This function can be called in any of the following ways:
But take a note that, the following function calls would be invalid:
By default, arguments may be passed to a Python function either by position or explicitly by keyword. For readability and performance, we can restrict the way arguments can be passed
So, a developer needs to look at the function definition to determine if items are passed by position, by position or keyword, or by keyword.
An advanced function definition may look like the one below:
If you're interested, check out special parameters python docs for more information.
What if you don't know how many arguments you want to be passed into your function?
Python provides us with a solution:
To specify the argument as an arbitrary argument, you need to just add a *(asterisk) before the parameter name in the function definition. The function, in turn, will receive the arguments and save it as a tuple of arguments, and you can access the items accordingly:
Output:
Largest number: 94
Similarly, to specify the argument as an arbitrary keyword argument, you need to add two asterisks: ** before the parameter name in the function definition. The function, in turn, will receive the arguments and save it as a dictionary of arguments, and you can access the items accordingly:
Output:
Marvel Studios presents - Iron Man
Starring - Robert Downey Jr.
Marvel Studios presents - Captain America: The First Avenger
Starring - Chris Evans
Marvel Studios presents - Thor
Starring - Chris Hemsworth
Also published at https://dev.to/aswin2001barath/functional-programming-in-python-23ff