The start of my 30 Days of Code begins with Functions. I had already read about flow with For and While loops.
Today I read into Functions.
Functions are like independent programs within a program. And it is defined before it is ever called.
[python] def myFunction(): print('Hello world') [/python]
To utilize the function, it is called within the program or script using the function name:
[python] myFunction() [/python]
The purpose of a function is to return a value.
Within a function, local variables can be defined. Local variables only exist (scoped) within the function and are destroyed or forgotten after the function has completed. While local variables are scoped only within the function, there is a concept of global variables.
Global variables are scoped outside of a function and can be used inside of a function. Take the example below:
[python] def myFunction(): global myVariable print('Hello ' + myVariable) myVariable = 'Rowell' myFunction() [/python]
The global variable statement is used inside the function but the variable is assigned outside of the function, globally. The output is as follows:
[python] >>> myFunction() Hello Rowell [/python]