Python Forum
My program subtracts fractions, but for some reason isn't simplifying them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My program subtracts fractions, but for some reason isn't simplifying them
#6
A function such as 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()
Reply


Messages In This Thread
RE: My program subtracts fractions, but for some reason isn't simplifying them - by Gribouillis - Feb-02-2018, 07:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 580 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Reason of the output himanshub7 2 2,219 Apr-28-2020, 10:44 AM
Last Post: TomToad
  Code used to work, not anymore for no apparent reason? chicagoxjl 1 1,930 Jan-08-2020, 05:05 PM
Last Post: jefsummers
  comparing integers and fractions tantony 3 2,052 Oct-02-2019, 04:32 PM
Last Post: tantony
  Help for simplifying code mmk1995 8 4,281 Sep-24-2019, 02:04 PM
Last Post: perfringo
  simplifying a stack of elifs Skaperen 8 4,197 Aug-17-2019, 04:13 AM
Last Post: Skaperen
  Syntax error for a reason I don't understand DanielCook 2 2,470 Aug-07-2019, 11:49 AM
Last Post: buran
  Simplifying my code ilondire05 5 3,832 Jul-21-2019, 03:21 AM
Last Post: scidam
  "invalid syntax" for no apparent reason wardancer84 2 7,213 Oct-03-2018, 11:57 AM
Last Post: wardancer84
  for some reason this isnt working lokchi2017 3 2,746 Aug-06-2018, 12:24 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020