Feb-02-2018, 07:48 AM
A function such as
So the main function prepares the ingredients (a and b), operates the food processor and normally gets a result. In your case, you don't get a return value because instead of returning a value, the functions prints something. This is called a side effect. Your function would be better if it returned the fraction that it computes and this result could be printed by the main function.
Experience will tell you what is the "better" design. This is related to many things: whether the function does one task or several tasks, whether its code is short or not, whether it can be called in many different situations, whether it can be modified easily, etc.
Concerning variables, if a and b are defined in main()'s body, it doesn't make them global variables. They are local variables in the function main(). By the way there is no special function main() in python. Your code doesn't need one, the interpreter doesn't treat it differently from function spameggsham()
fractionSubtraction
is similar to a food processor. Calling the function is filling it with vegetables (the arguments), pushing the button and getting back the chopped vegetables (the return value). Obviously the vegetables are not parts of the food processor, they are temporarily necessary.So the main function prepares the ingredients (a and b), operates the food processor and normally gets a result. In your case, you don't get a return value because instead of returning a value, the functions prints something. This is called a side effect. Your function would be better if it returned the fraction that it computes and this result could be printed by the main function.
Experience will tell you what is the "better" design. This is related to many things: whether the function does one task or several tasks, whether its code is short or not, whether it can be called in many different situations, whether it can be modified easily, etc.
Concerning variables, if a and b are defined in main()'s body, it doesn't make them global variables. They are local variables in the function main(). By the way there is no special function main() in python. Your code doesn't need one, the interpreter doesn't treat it differently from function spameggsham()