Please enable JavaScript.
Coggle requires JavaScript to display documents.
Functions in Python - Coggle Diagram
Functions in Python
Functions allow code to be segmented into manageable blocks - this is performed by refactoring the code.
-
Functions can be a simple way of using a piece of code multiple times to make code tidier and more efficient.
Running a function within the script is rather easy - just call the function name such as back-button() and the functional code will work as if inline.
Passing variables
Passing variables is performed by inputting information into the brackets of the call. Inside the function we would tell the programs the order of variables for example: def roll(sides, dice) this is known as a positional argument.
-
It is possible to change the order of the arguments by manually typing out the variable name . This is called a keyword argument.
Doc strings can be used to document the function, this should be in form of a short description that matches the python documentation.
Refactoring
Refactoring is used to prevent repetition in the code. This may involve rewriting some code in order to make it more efficient, easier to understand and easier to maintain.
Specific values should be removed and pointed at variables, this is called generalisation
When a program is running python keeps track of the all names that have been created, this is stored in a symbol table which can be used by inspecting the vars() function.
-