A 100 Days of Code lesson on Python's four argument types and the return statement.
Four types are named: default arguments, keyword arguments, variable length arguments and required arguments. In the average function written with a and b, both a and b are required arguments, because no default value was given for either of them in the function definition.
A default argument is given its value in the definition itself, such as a as 9 and b as 1. Calling average with nothing then still works and gives 5, because the function assumes the default value whenever a value is not provided for that argument in the call.
The passed values win. With defaults a as 9 and b as 1, calling with 1 and 5 ignores 9 and 1, takes a as 1 and b as 5, and the answer is 3. Passing only a as 5 leaves b at 1, so 5 plus 1 by 2 prints 3.
You provide arguments as key equals value, so the interpreter recognises each one by the parameter name and the order does not matter. Writing b equals 9 and a equals 21 makes a 21 and b 9. With plain arguments you must give a first and then b.
Required arguments, the ones with no default. If a equals 9 is not written in the definition, a becomes required and you cannot escape giving its value, while b still falls back to its default. Without key equals value syntax, arguments must be in the correct positional order and match the definition.
Define it with a star before the parameter: def average star numbers. Inside, initialize sum from 0, write for i in numbers with sum equal to sum plus i, then print sum divided by length of numbers. Passing 5, 6, 7, 15 or 25 numbers causes no problem.
It takes the numbers as a tuple. Calling average with 5 and 6 gives 5.5, and printing the type of numbers shows class tuple, so the values passed in arrive collected as a tuple. The difference between a tuple and a list is left for a later lesson.
Write double star before the parameter, as in star star name. The key equals value pairs passed to that function then arrive inside it as a dictionary called name, and printing the type of name prints class dict. The example prints hello with first name, middle name and last name.
Return sends the computed value back to the calling code instead of printing it. Writing return in average, then c equal to the call and print c, gives 4.75 for the numbers 5, 6, 7 and 1. Without a return you get none, because the function returns nothing.
Only the first return is considered; the second, and every return placed after it in the function, is ignored. Whichever return is reached first finalises the value and the function goes back. Return means go back with that value, so returning 7 makes the call give 7.
“Default arguments, keyword arguments, variable length arguments”
“We can provide a default value while creating a function.”
“What you can do is change the order of the arguments.”
“Required arguments are those arguments that you have to give.”
“So what happened is that it took numbers as a tuple.”
“Return means go back with this value.”
“Function Arguments in Python | Python Tutorial - Day #21.” https://www.youtube.com/watch?v=0d6b6fFuCkE. Transcript summary by WhipScribe, https://whipscribe.com/transcript-pages/function-arguments-in-python-python-tutorial-day-21.html.