• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

general question about programming

Hi. I have a general question about programming. I am wondering when to break things into subroutines and how to recognize when it would be good to do that. Generally I find that I cannot figure this out and my programming becomes very messy and the means to proceed is not clear. I don't know how to think about structuring the program. It seems that for my (very basic) programs that there will be one routine which will call another routine. There is also the problem of passing a value to the second routine. I have learned that this could be done by calling the second routine with arguments from the first routine (this is possible because the second routine is called from within the first routine) and then using shift; to catch the values of the argument.
Still I am not sure about these kinds of issues. Are there any good references or pointers on this?
Thanks.
0
onyourmark
Asked:
onyourmark
  • 3
  • 3
1 Solution
 
TintinCommented:
Everyone has their own preferences, but when to create subroutines generally comes down to two factors:

1.  When you want to call the same code multiple times.

2.  When you want to break down a large program into smaller chunks.


For very basic programs, there's generally no need for any subroutines.


To avoid using global variables, you should always parse arguments to your subroutines.
0
 
onyourmarkAuthor Commented:
Hi. Thank you. What do you mean by "parse arguments to your subroutines."?
0
 
TintinCommented:
As in

subroutine_name("argument1", "argument2");

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
onyourmarkAuthor Commented:
Thanks.

How about
subroutine_name argument1, argument2;
0
 
TintinCommented:
() are almost entirely optional in Perl.

Note that you would need quotes around the arguments if they aren't variables, eg:

subroutine_name "argument1", "argument2";
0
 
onyourmarkAuthor Commented:
THANK YOU.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now