There are three different types of functions.
Built-in functions are always available to the user. Eg print()
preinstalled modules have functions that need to be imported. eg math.pi()
lastly, you have functions which you created in the current code.
Another word for calling a function is invoking.
You receive a NameError exception in the console if you try to invoke a function before it is defined.
You received a TypeError exception if you pass an argument into a function that doesn't take any arguments.
Parameters vs arguments:
parameters live inside functions (this is their natural environment)
arguments exist outside functions, and are carriers of values passed to corresponding parameters.
Hungarians announce their name in reverse order: Eg. Harrington Niall, Doe John
Positional parameters: Typically, when arguments are passed into function parameters, they do so respective of their position. Eg.
def add(x, y, z):\ print(x + y + z)\ add(3,4,5)
The above code will assign 3 to x, 4 to y and 5 to z. Because that is the way they are ordered when they are passed in the invocation.
Keyword argument passing: Alternatively, you can assign arguments to keywords representing the function parameters, from within the function invocation. Eg.
def add(x, y, z):\ print(x + y + z)\ add(z=10, x =7, y =19)
This will ignore positional parameters, and assign the parameters from directly within the invocation.\ I'm not too sure what would be the reasoning for using this type of argument passing over positional parameters.\ On another note, if you use a non-existing parameter name in the invocation, you will receive a TypeError exception.
You can use both keyword and positional parameter passing in an invocation, but you have to put positional arguments before keyword arguments. If you fail to do this, you will receive a SyntaxError.
You can create a default parameter from within the parentheses of a function being defined. Eg.
def introduction(first_name, last_name="Smith"):\ print ("My name is: " + first_name + last_name)
introduction("Niall")
CONSOLE:\ My name is: Niall Smith
Notice how I didn't need to enter a second argument into the invocation. That's because the default second parameter was already set to "Smith". If I had entered a second argument, it would have overwritten the second parameter.
You can use the return keyword to end a function before it reaches the last line in the body, if the specified conditions are met. To me, this sounds very similar to the 'break' keyword.
Ok I understand it now. The return keyword comes in two different forms.
return without and expression will break out of a function.
return followed by an expression will return the expression's value as the function's result.
I broke away from the course to make a quick leap year tester program.\ https://github.com/GetOutOfThatGarden/LeapYearTester\ I'm still trying to get my head around git. I had an issue with having to overwrite the changes from the last push. I know I talked about this in the blog a couple of weeks ago.\ I'll need to spend more time using git.
I also need to figure out how to run python code in the console. So far I've been running the code in Codecademy's IDE, Edube's Sandbox, or IDLE.
With GO, I just had to enter 'go run {filename.go} into the terminal.