Parameters

Python bootcamp #100DaysOfCode #myNotes

The difference between Arguments and Parameters.
Parameter = the name of the data that’s passed into the function.
Argument = the actual value of the data.

Here are some examples:

Function without parameters.

def greet():
    print("Hello!")

greet() #will print Hello! 

Function with 1 parameter.
When calling the function, the parameter ‘name’ will be replaced
with the actual value (argument) ‘Lucia’

def greet(name):
    print(f"Hello {name}!")

greet("Lucia") #will print: Hello Lucia!

Functions with multiple parameters.
In this example the parameters are ‘name’ and ‘location’.
Now, beware that if you do it like this, the order of the arguments must match the order of the parameters.

def greet(name, location):
    print(f"Hello {name}! What is it like in {location}?")

greet("Lucia", "Paris") #will print: Hello Lucia! What is it like in Paris?

If you switch the arguments in the example above, the output would be Hello Paris! How is it like in Lucia? That’s not what you wanted right? You can omit this by using ‘keyword arguments’

Function call with keyword arguments.

greet(name="Lucia", location="Paris")

Will give you the same results as:

greet(location="Paris", name="Lucia")

Both will print: Hello Lucia! What is it like in Paris?

Now I only wish I was in Paris… a girl can dream right?


Geplaatst

in

,

door

Reacties

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.