+ 4
Why I should use user define function?
pleas give me short and sweet explaination
5 Answers
+ 3
1.) To prevent code repetition.
do_stuff(565)
do_stuff(80)
do_stuff(24789)
is shorter than copypasting the whole code of do_stuff each time.
2.) Easier to read.
+ 2
because you want to create your own wheels for the vehicle
+ 2
Lothar thanks
+ 2
User defined functions can make your programs shorter and more human-readable.
Functional and object oriented programming supports dividing programs into different tasks.
You can for example imagine walking, you could define walk method:
def walk():
...
It could contain thousands lines of code, but only its name could describe a lot about what the function does.
If you never touched this function, you would already have an image about that walk function would do some tasks related to walking, and this increases readability of code, if functions have welldescribing name.
You caould also:
def walk():
left_leg_up()
left_leg_middle()
left_leg_down()
right_leg_up()
left_leg_back()
right_leg_forward()
right_leg_down()
where the function is also divided into different tasks and it can also increase readability, when each function name gives a scheme about what the function could possibly do.