As I discussed in my last post you can actually use multiple parameters within the functions you create, it is important to note that the parameters within a function are essentially like variables that can only be used within the function they are declared in. You can even reuse the name of parameters within your function as variables within your code without any issues.
Code:
def myName(name):
print(“My name is”, name)
name = “John”
myName(“Joseph”)
print(name)
Result:
My name is Joseph
John
You can even have multiple parameters within the same function
Code:
def sum_of_ABC(A, B, C):
print(A, “+”, B, “+”, C, “=”, A + B + C)
sum_of_ABC(1, 2, 3)
Result:
1 + 2 + 3 = 6
So what do you do if you have one parameter that is always going to remain the same? Well you can actually set that parameter within the function, it is important to note that if you are setting parameters within the function it will need to be done after all other positional parameters, example below.
Code:
def name(firstName, lastName=”Gordon”):
print(“My name is”, firstName, lastName)
name(“Joseph”, “Smith”)
name(“Joseph”)
Result:
My name is Joseph Smith
My name is Joseph Gordon
If the function was “def name(lastName=”Gordon”, firstName):” you would have received a syntax error. As always if there are any questions, feel free to reach out!