+ 3

Are functions useful for making a code clean?

s.b. sayed me, that my program would be more readable if I put different parts of the code into several functions. But I think it is a memory waste, and a comment (or docstring ) would be more complete and better. What do you think?

1st Nov 2019, 12:42 PM
Gwlanbzh
Gwlanbzh - avatar
11 odpowiedzi
+ 8
Functions or methods are useful and yes more readable. It divides your program into parts for good. What I think is, without functions or methods ,your program can be cumbersome. What if you need a part of a code many times in your program, you will write it again and again? Then it will waste more memory and processor consumption will be more too. Comments are useful too.
1st Nov 2019, 12:48 PM
Chetali Shah
Chetali Shah - avatar
+ 4
SpaceNerd functions not only increase the readability of the code but it also helps to debug your code, if you writhe all the code in main it will be very difficult for you to find error occurred n large programs but when using function it is way more easier Also if you want a specific set of statements to use again and again you should use functions insted of writing that set of statements again and again
1st Nov 2019, 12:49 PM
Arsenic
Arsenic - avatar
+ 3
Writing everything in main() will make your main() too big. And then finding out error will become more difficuilt. Also for example, you want to add something in your code or modify something. Going through whole main() method will be frustating rather then searching for that particular function. Also, for example if something depends upon user's choice. Like user wish if he wants to multiply or add or divide ... Then the program will go nd check every condition mentioned ... While using function, straightway that function will be called. It is a basic example. Your code can be more complex too.
1st Nov 2019, 1:10 PM
Chetali Shah
Chetali Shah - avatar
+ 3
Yes because it keeps your code concise by avoiding repetition.
3rd Nov 2019, 4:28 AM
Sonic
Sonic - avatar
+ 1
Chetali Shah Hotboytrue ok. But in the program I talk about, what may be inside functions is used only once. Else, yes I would put it into functions without any question.
1st Nov 2019, 1:04 PM
Gwlanbzh
Gwlanbzh - avatar
+ 1
Also if any line of your code is having error then rest of your program will get stucked. But in case of function, only the function will be stucked ... Rest code will have no effect of that function on it.
1st Nov 2019, 1:13 PM
Chetali Shah
Chetali Shah - avatar
+ 1
If you did not have functions, to recycle repeating parts of code you could have had to do: func_index = None while exit_condition: if func_index == 0: #code elif func_index == 1: #code elif func_index == 2: #code #code
1st Nov 2019, 1:46 PM
Seb TheS
Seb TheS - avatar
+ 1
There are times when you'll need the same lines on different part of your program. Breaking them into smaller functions will save you from typing the same multiple lines again and again. If you have the needed lines in specific function, you'll only need to call that function name from another part in your program. Which one is better for you? Retyping multiple lines again, or just calling that function name (which will be one line)?
3rd Nov 2019, 4:47 AM
Alexoah YT
Alexoah YT - avatar
0
Yes functions you less lines because all you need to do is call it when needed instead of typing the same code over and over.
1st Nov 2019, 12:57 PM
Inkosi
Inkosi - avatar
0
You can call the same function multiple times throughout a program.
1st Nov 2019, 1:06 PM
Inkosi
Inkosi - avatar
0
I guess it depends on the language.
1st Nov 2019, 1:13 PM
Inkosi
Inkosi - avatar