A subroutine (e.g. procedure or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming languages includes support for creating self contained subroutines, and for calling and returning from them. They are in many ways similar to functions, but usually have side-effects outside of the simple "return value" that functions return.
Questions tagged [subroutine]
1255 questions
66
votes
7 answers
Syntax: "Exit Sub" or "Return" in VB.NET subroutines
Both "Exit Sub" or "Return" seem to accomplish the same thing -- exit a subroutine. Is there any difference in how they work under the covers?
That is,
Private Sub exitNow()
Exit Sub
End Sub
or
Private Sub exitNow()
Return
End Sub

Jeff
- 8,020
- 34
- 99
- 157
54
votes
4 answers
When should I use the & to call a Perl subroutine?
I have heard that people shouldn't be using & to call Perl subs, i.e:
function($a,$b,...);
# opposed to
&function($a,$b,...);
I know for one the argument list becomes optional, but what are some cases where it is appropriate to use the & and the…

user105033
- 18,800
- 19
- 58
- 69
37
votes
5 answers
Pass array and scalar to a Perl subroutine
Possible Duplicate:
How do pass one array and one string as arguments to a function?
I have a function, or subroutine, that takes in the first parameter as an array and the second parameter as a scalar. For example,
sub calc {
my @array =…

E.Cross
- 2,087
- 5
- 31
- 39
34
votes
2 answers
How to alias a function name in Fortran
Not sure if the title is well put. Suggestions welcome.
Here's what I want to do. Check a condition, and then decide which function to use in a loop. For example:
if (a < 0) then
loop_func = func1
else
loop_func = func2
endif
I can then use…

Samuel Tan
- 1,700
- 5
- 22
- 35
33
votes
1 answer
Basic Structure of a Haskell Program
Many of the Haskell tutorials I've looked through focus almost entirely on syntax with very little coverage on how to structure a program.
For example...
Here's a bare-bones outline of a C++ application:
#include
using namespace std;
int…

subtlearray
- 1,251
- 1
- 11
- 23
32
votes
3 answers
excel vba call subroutine with variables
I defined the following subroutine:
Sub EnterCellValueMonthNumber(cells As range, number As Integer)
range(cells).Select
ActiveCell.FormulaR1C1 = number
End Sub
When I call the subroutine like this:
EnterCellValueMonthNumber ("N23:Q23",1)
I get…

user366121
- 3,203
- 9
- 48
- 69
32
votes
9 answers
Perl - Subroutine redefined
I have asked this question before or searched and seen others ask - why am I getting the warning "Subroutine mySub redefined at ../lib/Common.pm line x"? and you always get the answer you declared the sub twice in the same code. I created this test…

user210757
- 6,996
- 17
- 66
- 115
31
votes
1 answer
Assembly 'call' vs 'jmp'
I got told to try and use 'jmp rather than 'call', but 'jmp' is not liking me .. when I jump it doesn't return (so it never exits and not happy days ), but calling returns and exits as normal.
I am happy using 'call' but is there actually a reason…

user3502489
- 361
- 1
- 4
- 11
28
votes
3 answers
Correct use of modules, subroutines and functions in Fortran
I've recently learnt about interface blocks when adding a function to my Fortran program. Everything works nice and neatly, but now I want to add a second function into the interface block.
Here is my interface block:
interface
function…

AncientSwordRage
- 7,086
- 19
- 90
- 173
28
votes
5 answers
How do I pass a hash to subroutine?
Need help figuring out how to do this. My code:
my %hash;
$hash{'1'}= {'Make' => 'Toyota','Color' => 'Red',};
$hash{'2'}= {'Make' => 'Ford','Color' => 'Blue',};
$hash{'3'}= {'Make' => 'Honda','Color' => 'Yellow',};
&printInfo(%hash);
sub…

masterial
- 2,166
- 9
- 33
- 48
28
votes
3 answers
Excel VBA calling sub from another sub with multiple inputs, outputs of different sizes
I would like to call a sub from another sub inside in the same module. The first sub would be my main code and there I would call the second subroutine. Second subroutine receives multiple inputs as integer, double, double arrays and double…

user2421555
- 321
- 1
- 3
- 6
26
votes
5 answers
How can I use hashes as arguments to subroutines in Perl?
I was asked to modify some existing code to add some additional functionality. I have searched on Google and cannot seem to find the answer. I have something to this effect...
%first_hash = gen_first_hash();
%second_hash =…
user130532
24
votes
3 answers
What's the best way to discover all subroutines a Perl module has?
What's the best way to programatically discover all of the subroutines a perl module has? This could be a module, a class (no @EXPORT), or anything in-between.
Edit: All of the methods below look like they will work. I'd probably use the …

Robert P
- 15,707
- 10
- 68
- 112
23
votes
2 answers
How to define a subroutine in PowerShell
In C# a RemoveAllFilesByExtenstion subroutine could be, for example, decleard like this:
void RemoveAllFilesByExtenstion(string targetFolderPath, string ext)
{
...
}
and used like:
RemoveAllFilesByExtenstion("C:\Logs\", ".log");
How can I defne…

Maxim V. Pavlov
- 10,303
- 17
- 74
- 174
23
votes
4 answers
When should I use subroutine attributes?
I don't grok Perl subroutine attributes at all.
I have never seen them in actual code and perldoc perlsub and the perldoc attributes fail to answer my questions:
What are attributes useful for?
What do they bring to the table that is not already…

Zaid
- 36,680
- 16
- 86
- 155