Arguments and Parameters in PythonBe it any programming language, Arguments and Parameters are the two words that cause a lot of confusion to programmers. Sometimes, these two words are used interchangeably, but actually, they have two different yet similar meanings. This tutorial explains the differences between these two words and dives deep into the concepts with examples. Both arguments and parameters are variables/ constants passed into a function. The difference is that:
Example:Output: Enter the value of the first number: 5 Enter the value of the second number: 2 Sum of two numbers: 7 Points to grasp from the Example:
Mechanism:Observe that in the above example, num1 and num2 are the values in the function call with which we called the function. When the function is invoked, a and b are replaced with num1 and num2, the operation is performed on the arguments, and the result is returned. Functions are written to avoid writing frequently used logic again and again. To write a general logic, we use some variables, which are parameters. They belong to the function definition. When we need the function while writing our program, we need to apply the function logic on the variables we used in our program, called the arguments. We then call the function with the arguments. Types of Arguments:Based on how we pass arguments to parameters, arguments are of two types:
Example: Output: Details of student: Raghav age: 12 grade: 6 Details of student: Santhosh age: 12 grade: 6 Points to grasp from the Example: First Function Call:
The arguments are passed position-wise to the parameters, which mean according to the passed order:
Here, the first argument, "Santhosh", is passed based on its position to name, and the next two arguments are passed by assignment to their respective parameters. As you can observe, here, the position didn't matter. Important Point:
If we write: details("Santhosh", age = 6, 12) Call by Value and Call by Reference:This is the most important concept on arguments and parameters. Based on the type of arguments passed to the parameters, there are two methods of invoking/ calling functions-Call by value and Call by reference. When the values of arguments are passed into parameters in the function, the values are copied into parameters. This method is called "Call by value". In this method, arguments and parameters are different and are stored in different memory locations.
When the addresses of the arguments are passed into parameters instead of values, this method of invoking a function is called "Call by Reference".
Which Method does Python Follow?Python doesn't use Call by value or Call by reference. It follows a method called "Call by assignment". In Python, every single entity is an object. Objects are divided into Mutable and Immutable objects. What happens in Python when we assign a value to a variable is different from other low-level languages like C or Java. Suppose, in the statement: a = 20 a is the variable, and 20 is the value assigned. Here in a memory location, 20 is saved, and a is the name we're giving to the reference we're making to the memory location. Now, if we say: a = 21 The name stops referring to the memory location with 20 and starts to refer to another memory location with 21. In other languages like C, variables are the memory locations that store the values. Example:In C: Output: 000000000062FE1C 000000000062FE1C In Python: Output: 140714950863232 140714950863264
Now comes the concept of Mutable and Immutable objects in Python. Mutable and Immutable Objects in Python:
Example: Mutable Objects: Output: 2253724439168 2253724439168 Understanding: A list is immutable, which means we can alter or modify it after creating it. As you can observe, when created with the name a, it is saved in the address "2253724439168". Using append(), we altered it by appending another value. It is still in the same memory location, meaning the same object is modified. Immutable Objects: Output: 140714950863232 140714950863968 Understanding: This is the case we discussed before in the tutorial. An int object is immutable, meaning we can't modify it once created. You might wonder we still added 23 in the above code. Observe that the object when created is not the same object after adding. Both are in different memory locations which means they are different objects. So, how are arguments passed to the parameters when a function is invoked? With all the knowledge about assignment operation in Python:
Example: Output: Details of the student: name: Harry Styles age: 15 grade: 10 marks: [25, 29, 2F1, 30, 26] 10 [25, 29, 21, 30, 26] Understanding: The function accepts 4 arguments. Notice the arguments grade and marks. grade is an integer value which means it is immutable. Hence, once created, we can't modify it. It follows "Call by Value". As we discussed earlier in the tutorial, when following Call by reference, "Changes done on the parameters (pointers) will not affect the values of the arguments in the program". Hence, the original value of grade in the program is not modified after concatenating the string in the function definition. In the case of marks, it is a list and is mutable. So, it follows "Call by Reference," which means, "Changes done on the parameters (pointers) will affect the values of the arguments in the program". Hence, the change is reflected in the original program after appending the list in the function definition. Next TopicAttributes Meaning in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India