Functions are a very important tool for you to use when programming. They will save you a great deal of time, improve your code structure and help you avoid making mistakes. Throughout this lesson we’ve covered
- What functions are and what they are for
- Function parameters and return values
- Defining functions
- Using our functions for code organization
We can organize and remove repeated code by moving it to functions. Once the functions are written then they can be called from other parts of your program, using the function name.
Parameters and Returned Values
Function parameters enable us to pass data to functions, making the function able to change what it does based on the input.
Functions also enable you to return values back to the code that calls the function.
When we define a function, we specify its name, code, and whether it has any parameters and returned values.
def calculate_average(num1, num2): return (num1 + num2) / 2
Functions for Organization
Functions are also very useful for breaking your code into structured, logical blocks. Using functions, we can organize our programs into more readable function calls.
def main(): random_bag = create_random_bag() display_bag()
That’s it! When creating programs, consider using functions to organize their logic.
This exercise is designed to be an open playground. Try moving the goal around, defining functions, or using the functions provided.
Define A: Move Right Move Down Call Function A Call Function A Call Function A Call Function A Move get path to 2 6