Function with Parameters
The true potential of a function becomes apparent when it can receive data from the user. This data can change how the function behaves, making it more versatile and capable of adapting to different situations.
A parameter in a function is similar to a variable, but it has two important characteristics:
- Parameters exist only within the function where they are defined. They cannot be accessed from outside the function. To define a parameter, it must be declared between the parentheses in the def statement.
- The value of a parameter is assigned when the function is called, by providing a specific argument that corresponds to the parameter. This argument determines the initial value of the parameter inside the function.
def function(parameter):
###
Remember!
- Parameters reside within functions and are designed to be used inside them. It's their natural environment.
- On the other hand, arguments exist outside functions and carry values that are passed to the respective parameters.
- There is a distinct boundary between these two worlds, keeping them separate and clear.
Now, let's break down the process of adding a parameter to a function and using it in our code, step by step.
Adding a Parameter to a Function
Let’s say we want to create a new function called laugh()
that display laugh 'Ha!
' as shown below:
def laugh():
print(“Ha!”)

Now, we will enhance this function by adding a parameter called num
. And then, we will call the function and see the output. let's run the code.
def laugh(num):
print("Ha!")
laugh(3)
The laugh function has been defined with a parameter called num
. However, in the current implementation, the num parameter is not being used within the function's body. Instead, the function simply prints "Ha!
" when called.
When we call the laugh(3)
statement, the function is invoked with the argument 3. Although the num
parameter exists in the function definition, it is not utilized within the function's logic. As a result, the output of the code will be:
In this case, the value of num does not have any effect on the output because it is not used within the function. Thus, regardless of the value passed as an argument when calling the function, it will always print "Ha!
".
In the previous example, we used the parameter, but it's important to note that we didn't assign any value to it explicitly. However, this is perfectly fine.The value for the parameter will be provided from the function's environment when it is invoked.
It's essential to remember that when defining a function, specifying one or more parameters is a requirement. And when you invoke the function, you must fulfill this requirement by providing arguments that correspond to the defined parameters.
Failing to provide the required number of arguments will result in an error. For instance:
def laugh(num):
print("Ha!")
laugh()
The above code will resulted in error. When you attempt to call the laugh()
function without providing the required argument, it will result in a TypeError. The error occurs because the function definition specifies that the num
parameter is required, meaning the function depends on receiving a value for num
in order to execute correctly.
Invoking the Function with Parameter

Now, let us enhance the function by utilizing the num
value. Let's say, in this case, we want to display the number of times, maybe a baby laughs which is represented by num
. Inside the function, we print the provided number of laughs along with a message. Then, using a loop, we print "Ha!
" as many times as specified by the num parameter. To accomplish this, we need to modify the "def
" statement. Here's the updated version:
def laugh(num):
for i in range(num):
print("Ha!", end=" ")
laugh(3)
Note: The parameter end=
takes care of removing the newline that is added by default in print()
The laugh function is defined to take one parameter called num
. When the function is invoked, it enters a loop that iterates num
times. During each iteration, it prints "Ha!
" to the console.
In the last line of the code, laugh(3)
is called, passing the argument 3 to the laugh
function. This means that the function will be executed with num set to 3. As a result, the function will iterate three times in the for loop, printing "Ha!
" on each iteration.
The output of the code will be:
Since num
is 3
, the for
loop iterates three times, printing "Ha!
" on each iteration. This demonstrates how the function can be invoked and the behavior of the loop based on the value of the num
parameter provided during the function call.
By passing different values to laugh, we can control the number of "Ha!
" prints in the output. For example, laugh(5)
would print "Ha!
" five times. The num
parameter allows flexibility in customizing the behavior of the laugh function.