Functions with Inputs
We can execute same lines of instruction for different data by passing different input values to the function.
def greet_with_name(name): #Function Definition
print(f"Hello {name}")
print(f"How do you do {name}")
greet_with_name("Maryam")# Function Call
You can pass any other name. You do not have to rewrite these instructions again.
def greet_with_name(name):
print(f"Hello {name}")
print(f"How do you do {name}")
greet_with_name("Jack")
Parameters vs Arguments
A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that are sent to the function when it is called.
def greet_with(name , location): #Parameter
print(f"Hello {name}")
print(f"Where do you live {location}")
greet_with("Maryam" , "Attock")#Argument
Positional Vs Keyword Arguments
In positional arguments, if we switch the places of arguments it will assigned to the different parameter. For example below, argument "Maryam" is assigned to the parameter name and argument "paris"will be assigned to the parameter location.
#Positional argument
def greet_with(name , location):
print(f"Hello {name}")
print(f"Where do you live {location}")
greet_with( "Maryam" , "Paris")
But is we switch arguments as "Paris" , "Maryam" then "Paris" will be assigned to name and "Maryam" to location.
def greet_with(name , location):
print(f"Hello {name}")
print(f"Where do you live {location}")
greet_with("Paris" , "Maryam")
In Keyword argument, position of arguments do not impact.
#Keyword argument
def greet_with(name , location):
print(f"Hello {name}")
print(f"Where do you live {location}")
greet_with(location = "Paris" , name = "Maryam")
Exercises
We have done 4 exercises on day 5
Paint Area Calculator
Prime Number Checker
Head over to my GitHub repository to see their codes.
Project
We have done a project of Caesar Cipher. The Caesar Cipher technique is one of the earliest and simplest method of encryption technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example with a shift of 1, A would be replaced by B, B would become C, and so on. The method is apparently named after Julius Caesar, who apparently used it to communicate with his officials. The project is in GitHub repository.