Questions tagged [subroutine]

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.

1255 questions
15
votes
4 answers

How to handle subroutine redefined errors in Perl

So I have a file that in short has this problem... #!/usr/bin/perl -w package Foo; use strict; use POSIX; ... sub remove { ... } ... and I get a get an error saying the subroutine remove has been redefined. I know the problem, there is a…
floogads
  • 271
  • 2
  • 4
  • 13
15
votes
5 answers

Can Perl method calls be intercepted?

Can you intercept a method call in Perl, do something with the arguments, and then execute it?
Geo
  • 93,257
  • 117
  • 344
  • 520
14
votes
3 answers

How can I selectively access elements returned by a Perl subroutine?

Say a Perl subroutine returns an array: sub arrayoutput { ...some code... return @somearray; } I want to access only a specific array element from this, say the first. So I could do: @temparray=arrayoutput(argument); and then refer to…
Abhranil Das
  • 5,702
  • 6
  • 35
  • 42
14
votes
7 answers

Passing two or more arrays to a Perl subroutine

I am having trouble passing and reading arguments inside subroutine which is expected to have two arrays. sub two_array_sum { # two_array_sum ( (1 2 3 4), (2, 4, 0, 1) ) -> (3, 6, 3, 5) # I would like to use parameters @a and @b as simply as…
eold
  • 5,972
  • 11
  • 56
  • 75
14
votes
9 answers

Default argument values in subroutines

I don't know how to set default arguments for subroutines. Here is what I considered: sub hello { print @_ || "Hello world"; } That works fine for if all you needed was one argument. How would you set default values for multiple arguments? I was…
David
  • 592
  • 3
  • 6
  • 13
14
votes
5 answers

Should I call Perl subroutines with no arguments as marine() or marine?

As per my sample code below, there are two styles to call a subroutine: subname and subname(). #!C:\Perl\bin\perl.exe use strict; use warnings; use 5.010; &marine(); # style 1 &marine; # style 2 sub marine { state $n = 0; # private,…
Nano HE
  • 9,109
  • 31
  • 97
  • 137
14
votes
2 answers

batch script subroutine: Passing arguments

My understanding is that in order to get the date from a file passed into a subroutine as an argument, you must re-set that argument as a variable within the subroutine. Is this correct? This doesn't make since to me, so I am wondering if I do not…
Fractal
  • 1,748
  • 5
  • 26
  • 46
14
votes
4 answers

What is the difference between a subroutine and a function?

Possible Duplicate: What is the difference between a ‘function’ and a ‘procedure’? I searched online for an answer to this question, and the answer I got was that a function can return a value, modify a value, etc., but a subroutine cannot. But I…
CodeBlue
  • 14,631
  • 33
  • 94
  • 132
13
votes
3 answers

Does Fortran preserve the value of internal variables through function and subroutine calls?

After much painful debugging, I believe I've found a unique property of Fortran that I'd like to verify here at stackoverflow. What I've been noticing is that, at the very least, the value of internal logical variables are preserved across function…
EMiller
  • 2,792
  • 4
  • 34
  • 55
13
votes
2 answers

VBA Pass Array By Reference and Modify Contents

VBA question. I'm using it to make Solidworks macros, but that's not important. Can someone explain the syntax to pass an array (1-Dimensional of type Double, with length of three) to a subroutine or function so I can modify the contents. I know…
Geoffrey
  • 143
  • 1
  • 1
  • 4
13
votes
1 answer

Perl modifying hash reference in subroutine

I am having trouble understanding the hash references and changing the hash in place, instead of returning it. I want to write a sub routine which will return a value from hash and also modify the hash. I was facing some issues while coding for it.…
Sid
  • 317
  • 1
  • 2
  • 8
12
votes
1 answer

What's the primary difference between branch instruction and branch with link instruction? ARM?

From what I read, the branch with link is used to perform subroutine call and the registered link is copied back to PC.
夢のの夢
  • 5,054
  • 7
  • 33
  • 63
11
votes
2 answers

How can I return 2 hashes from a raku sub?

... #!/usr/bin/env raku # -*-perl6-*- # 2021-5-30: Example of how a sub does not seem to be able to return 2 Hashes... sub GetHashes { my %H = 100 => 2149, 101 => 2305, 102 => 2076, 103 => 1767, 104 => 1743 ; my %G = 100 => 21493, 101…
GRM
  • 119
  • 2
11
votes
2 answers

Using CALL for labels in a batch script

When using the CALL command to call a label in a batch script, and you end the sub-routine with GOTO:eof, what happens from there? Does it return back to where the sub-routine's CALL is located? Or does it continue on after the location of the call…
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
11
votes
4 answers

How can I inline Perl subroutines?

I am reading Code Complete 2, and one of the points mentioned is about creating subroutines even for operations that seem too simple to have their own subroutines, and how that can be helpful. I know I can inline functions in C and C++ using the…
Lazer
  • 90,700
  • 113
  • 281
  • 364
1 2
3
83 84