Learning Blog
Functions - December 18
Station 1: Construct Code Station
Before working on this problem, I did not have a deep understanding of how to write and define multiple functions to implement the complete functionality of a program. Through this problem, I learned how to define different functions to perform specific tasks, such as showMenu to display the menu and get the user's choice, add2Nums to perform addition, and minus2Nums to perform subtraction. I also learned how to call these functions within the main function main() and decide which function to use based on the user's choice. Additionally, I gained an understanding of how to handle user input, pass the input values to the functions for processing, and output the results. By using if statements, I learned how to control the flow of the program based on user input and execute different blocks of code. This process helped me better understand how to structure a program, making it more readable, understandable, and maintainable. By breaking down functionality into smaller functions, the program not only achieves the required tasks but also improves readability and maintainability. Overall, this problem helped me deepen my understanding of how to organize a program, define and call functions, handle user input and output, and manage program flow.
Station 2 Debugging Station
Before this station, I was not very skilled in defining and using functions, especially when dealing with function arguments and return values, and often made mistakes, such as forgetting to write colons, leaving Spaces between function names, or naming parameters and variables inconsistently. In addition, I do not know enough about the scope of variables (local and global), so when I modify variables, I cannot accurately determine which variables will be affected. These problems often cause code to run incorrectly or produce results that do not meet expectations. Through this exercise, I learned how to define functions properly, including making sure function names have no Spaces, adding colons after definitions, and keeping indentation consistent. At the same time, I have a better understanding of how parameters are passed and the role of return values. For example, I now know that if you need a function to modify an external variable, you should do so by returning a value rather than modifying the variable directly inside the function. I also learned how to use variables in different scopes to avoid confusion when variables with the same name are used. Through this exercise, I gained a deeper understanding of function definitions, parameter passing, and variable scope, and became more confident in using functions to solve problems in my code. In the future, I will continue to consolidate these skills through practice, especially in the further application and optimization of more complex programs.
Station 3: Prediction Station
Before doing this exercise, I didn't know much about how functions handle arguments and return values, for example, I wasn't sure if changing a variable in a function would affect outside variables, and I didn't know that Python passed values. Also, the distinction between local and global variables is a bit vague to me, so it's often unclear what exactly the code is going to output. After doing this exercise, I understand that changes in a function generally do not affect the variables outside, unless the result is communicated by a return value. I also made it clear that local variables are only valid in functions, separate from global variables. Now I can analyze the function step by step and determine exactly what the output of the code will be. Through this exercise, I have a clearer understanding of how functions work, and I feel more confident analyzing code. I think as long as I practice and think more, I can gradually master the knowledge.
Reflection
When writing functions, beginners often make common errors due to a lack of experience with Python’s syntax and logic. These include syntax mistakes like missing colons or incorrect indentation, which can lead to errors such as IndentationError. Name and argument errors, such as misnaming variables or passing the wrong number of arguments, are also common. Logical errors, such as expecting changes to immutable variables (like integers) to persist outside the function or forgetting to return values, are frequent as well. To reduce these mistakes, beginners should practice consistent indentation, use descriptive names for functions and variables, and test functions frequently. It’s also important to understand mutable versus immutable types and return values when needed. Breaking code into smaller functions, documenting code clearly, and using debugging tools can help catch errors early. By following these practices, beginners can write clearer and more reliable code.
0 being “no idea”; 5 being “I feel can confidently do it in any situation”
4 out of 5 - We are learning how to create a function
4 out of 5 - We are learning how to create a function with parameters; that accepts arguments (input values)
4 out of 5 - We are learning how to create a function with a return value
3 out of 5 - We are learning the scope of a variable (local vs global)
3 out of 5 - We are learning how to create our own functions to make our code more reusable (efficient)
I feel this way about scope of a variable and creating reusable functions because while I understand the concepts, I still find it challenging to manage scope in more complex code. Sometimes I forget that variables with the same name in different scopes can conflict or lead to bugs, especially when I’m working with larger programs. Also, while I can create functions to make my code more efficient, I struggle to determine when it’s best to refactor code into reusable functions rather than just writing everything in a single block.
To improve my ability in these areas, I plan to practice by working on more projects that involve multiple functions and variable scopes. I’ll pay close attention to how variables are scoped and ensure I use global and local variables appropriately. I will also focus on writing functions that can be reused in different contexts, making sure they are modular and easy to integrate into other parts of my code. Additionally, I will review my code for places where I could improve efficiency through reusable functions, and seek feedback from others to identify areas where I can improve. Through consistent practice and learning from others, I believe I can become more confident in these areas.