Arguments are values to be passed into a function when the function is being called. It is commonly used interchangeably with a parameter which is a placeholder specified during the definition of a function.
Generally, functions are used to bundle a set of instructions that you want to use repeatedly. They might be more complex but the code samples below would serve as simple examples.
def contact(name,number):
return(f"Contact details are as follows: Name; {name} Number;{number}")
contact("Toyin", "0703000000")
>>> Contact details are as follows: Name; Toyin Number; 0703000000
According to the function defined above, name and age are parameters holding the place of the arguments to be called. “Toyin” and “07035000000” are the arguments that have been passed into the parameters name and age respectively.
Types of Arguments
These are the types of arguments and we will be discussing the following:
- Required Arguments
- Positional Arguments
- Optional Arguments
- Keyword Arguments
REQUIRED ARGUMENTS
Just as the name states, required arguments are arguments that must be passed to a function for the code to run. Here we have a sample code that shows you how required arguments function:
def foobar(rndm):
return(f"a random word: {rndm}")
foobar("cup")
>>> a random word: cup
foobar()
>>> TypeError: foobar() missing 1 required positional argument: 'rndm'
Without the argument, the function gives an error with a message prompting you to use an argument.
POSITIONAL ARGUMENTS
Positional arguments are arguments that have to be called in the order their parameters were created when the function was defined. Let's look at an example below:
def stutern(tutor,cohort):
return(f "{tutor} is my tutor and {cohort} my fellow cohort")
stutern("George", "Ifeanyi")
>>> George is my tutor and Ifeanyi is my fellow cohort.
The function above gives a factual statement because the arguments were called following the positional order in which the parameters were set when the function was defined. If the order is not followed the results might not be correct.
stutern("Ifeanyi" "George")
>>> Ifeanyi is my tutor and George is my fellow cohort.
That's not quite right, is it? George is my tutor, not Ifeanyi.
Another Example using figures is below. When you switch the numbers, it gives you a different result because it follows the mathematical rule of executing the function in the Bracket before other mathematical functions.
def s_maths(a,b,c):
d= (a+b)*c # First sum a & b and multiply the result with c
return(d)
s_maths(2,4,3)
>>> 18
The above output gives (18) but once you switch the position of the 4 & 3 it gives a different output (20) shown in the code below.
s_maths(2,3,4)
>>> 20
OPTIONAL ARGUMENTS
Optional arguments usually have a default value set because their parameters do not always take in arguments. If the function is called without the argument it would pass its default value. Let's look at an example below:
def bio(name, nationality = "Nigerian"):
return(f"My name is {name} and I am a {nationality}")
bio(Toyin)
>>> My name is Toyin and I am a Nigerian
The above function was called without the last function and it was executed using the default value. Let, pass in a new argument in the example below.
bio("Toyin", "Canadian")
>>> My name is Toyin and I am a Canadian
When a new value is passed into the function it overwrites the default argument and gives different output.
KEYWORD ARGUMENTS
Keyword arguments are arguments that allow you to map the parameters with the arguments when calling the function. When we call functions in this way, the order (position) of the arguments can be changed. The following calls to the functions below are all valid and produce the same result.
Here is some code to show how this works:
def greet(greeting,name):
return(f"{greeting} {name} how are you doing?")
greet(greeting= "Hi", name ="Adnan")
>>> Hi Adnan how are you doing?
The above function if reversed would give the same output.
greet( name ="Adnan", greeting= "Hi")
>>> Hi Adnan how are you doing?
Learning about the types of arguments helps you decide on the appropriate argument type when defining a function and also helps you understand how functions take in arguments. This is one of the keys to creating more complex functions.
If you've gotten to the end, Thanks for reading! Please leave your comments