Bad form not to use functions in Python?

In summary, it is generally considered good programming practice to organize a program into smaller functions that have well-defined tasks and minimal data exchange. This applies to all languages, including Python. However, there is no set rule and the decision ultimately depends on the programmer's judgement.
  • #1
bigfoot100
4
0
I have a 100 line code which is liable to increase, so far I have not included any user defined functions, so it is one stream of code. In c I was taught to put everything I could into functions, is this the case in python i.e. would I be frowned upon for not using functions?

Many thanks.
 
Technology news on Phys.org
  • #2
Organizing a non-trivial program into a collection of functions that are named clearly, perform well-defined tasks and exchange the smallest amount of data necessary to perform those tasks while keeping other data "local" to each function, is a hallmark of an effective programmer, regardless of the language. For years, I've aimed for this goal in programming in Fortran, Pascal, C++ and Perl, and I see no reason why Python should be any different.
 
  • #3
Do whatever you think is right. There are obviously some good programming practices but don't blindly follow rules because you feel that's what you should be doing...

100 lines of code is not exactly huge. If there's no code to refactor and it makes sense to you, I'm sure it's fine...

Even without code reuse, breaking up a program into smaller functions called from a main routine (in Python, that'll just be the part of your program that actually executes things rather than just declarations) will allow you to see the overall structure of your program in one small(er) location. You can examine individual functions to see more implementation details. Of course, this works best if your functions have well defined roles and you'll want to keep their interactions as simple as possible, etc... but all this should come naturally to you as you code more...

Just do what you think works best and it should all work out, eventually...
...and yes, this is true regardless of language...
 

Related to Bad form not to use functions in Python?

1. What is the purpose of using functions in Python?

Functions in Python are used to break down a larger program into smaller, more manageable tasks. This makes the code more organized, easier to read, and allows for better reusability. Functions also help to avoid repetitive code and make debugging easier.

2. Can I write code in Python without using functions?

Yes, you can write code in Python without using functions. However, it is considered bad form as it can lead to longer and more complex code that is difficult to maintain and debug. Functions are an important aspect of Python programming and should be used whenever possible.

3. How do functions in Python improve code efficiency?

Functions in Python can improve code efficiency in several ways. They avoid repetition of code, making the code shorter and more concise. They also make the code more modular, allowing for easier testing and debugging. Additionally, functions can be reused in different parts of the code, reducing the need to write the same code multiple times.

4. Are there any disadvantages to using functions in Python?

One potential disadvantage of using functions in Python is that it can add extra layers of complexity to the code, especially for beginners. Also, if functions are not used properly, it can lead to poorly structured code and make debugging more difficult.

5. Can I use functions in Python for any type of programming task?

Yes, functions can be used for a wide range of tasks in Python programming. They are not limited to specific types of programming and can be used for tasks such as data manipulation, web development, and scientific computing. Functions are an essential aspect of Python and are used in almost all types of programming tasks.

Similar threads

Replies
6
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
1
Views
644
  • Programming and Computer Science
Replies
3
Views
474
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
25
Views
4K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top