First, you need to understand what functions are used for. You might say, that a function provides a service: It does something for you.
- Provide a service
- don't know you exist
A printer for example is some kind of a service: It prints documents for you, you don't have to do it yourself. If you provide the printer with the necessary information (the parameters), the printer will happily do its job.
The printer does not know what data it will receive, otherwise, it wouldn't need a parameter. The parameter is the "unknown" that is needed to perform the service.
def my_function(a, b):
This provides a service (or function) that wants two pieces of information from you. Once it has this information, it needs to be able to refer to it somehow. The notation
(a, b) simply means that whatever you give it as first "information", will accessible from the name
a and what you pass second will be accessible from the name
When you actually call the function (i.e. tell the printer to actually print something), you need to fill in these gaps of knowledge like so:
How does the function buildConnectionString with the argument (params) know to get it's data from the variable myParams?
It doesn't. Does my printer know I am going to print a document relating to programming tomorrow? No. Tomorrow, I will send the information to my printer, which it will then happily print. The printer does not even know I exist.
Similarly, you "send" (pass) the data (
myParams) to your function with the following expression:
At some other point in your program, you may wish to execute your function with different data. Similar to the printer, the function doesn't know who will call it, or when it will be called, or with what data it will be called.
Or params.items(), too within the return line?
The function, once executed, has received some data so it can do its job. This will execute a function called
items() on whatever has been given the function to work with.
An example function
I want to create my own service, that can calculate the square of a number. This implies, that it needs to be given a number in order to work. After all, you can't square nothing, can you?
The function does not know, which number it will be given. However, in order to be able to multiply this number, we need to be able to refer to it somehow, hence, we give it a name:
return x * x
This defines a function called square that needs to be given one piece of data (the
I can now use this function to square a number:
>>> print square(2) # "Please square this number 2 for me"
Within the function,
x now refers to the value
But I could also have called the function with the number 3:
>>> print square(3) # "Please square this number 2 for me"
I could have chosen the name
two instead of
x for my function. But that would be misleading: The value does not have to be
2, it can be whatever I wish it to be.
And is there a reason behind not naming your variable params, or vice
versa naming your argument in your function myParams?
Names in programming can be chosen arbitrarily, as long as they are not reserved by the language (these are called keywords).
Usually, you will name your variables whatever makes sense in that context. For instance, in code about geometric objects, my local variable may be named
area which I will then square using the
square() function. In code about a car I may have a variable named
weight on the otherhand. I am sure you will agree that I being able to call square with
square(area) conveys a lot of information.
Would you now change the parameter of
weight_or_area? I hope you wouldn't. What if I was to use the function in connection with
lengths? You will have to get used to the fact that parameters and arguments of functions are usually named differently, that is a Good Thing®.
It's just been
extremely confusing seeing the argument set as one word, and then
having the variable named something completely different, but having
the function still be able to read your code, even when the argument
name is different from your variable with the data you're printing
It is important to understand, that the function does not read your code. It has no idea it even exists. This brings us back to the printer analogy: Does it know you or I exist? Not, it's there to perform a job, no questions asked. The same thing applies to functions: They are there to do our bidding. They do not act on their own and "read code" to see where they are referenced.