Typically, functions / named blocks of code take a series of operands / parameters as inputs, and return a result which is saved into another variable - here's an example in Perl:
$size = length($persname);
Incoming value - read from
$persname
Action defined in -
length
Outgoing value - saved into
$size
However - I have said
typically, as in some languages it's possible for a function to amend the input values as well. Take a look at this Perl line:
$boots = chop($feet);
$boots gets set to the final character of the incoming string in
$feet which is modified by the removal of that character. So:
Incoming in $feet:
Hello[cr]
Outgoing in $boots:
[cr]
Outgoing in $feet:
Hello
Once you're aware of this action from the
chop function, it works very well for you ... but it comes as a bit of a surprise the first time you see it!
Complete source example (and sample output)
[here]. We cover this subject (and of course a lot more!) on our
Perl Programming course.
(written 2012-01-10, updated 2012-01-14)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P209 - Subroutines in Perl [96] Variable Scope - (2004-10-22)
[308] Call by name v call by value - (2005-05-11)
[357] Where do Perl modules load from - (2005-06-24)
[531] Packages in packages in Perl - (2005-12-16)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[775] Do not duplicate your code - (2006-06-23)
[969] Perl - $_ and @_ - (2006-12-07)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1921] Romeo and Julie - (2008-12-04)
[2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[3066] Separating groups of variables into namespaces - (2010-11-24)
[3833] Learning to use existing classes in Perl - (2012-08-10)
P202 - Perl Fundamentals [184] MTBF of coffee machines - (2005-01-20)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1726] Hot Courses - Perl - (2008-07-28)
[1946] Variable Types in Perl - (2008-12-15)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3329] Perl from basics - (2011-06-20)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
Some other Articles
A busy start to 2012How to do multidimensional arrays (or rather lists and hashes) in PerlTcl - apparently odd behaviour of string trimleftMultiple buttons calling the same proc in wish (tcl/tk)Perl functions such as chop change their input parametersNew in Java 7 - and why we are not running public Java 7 coursesAdding Expect on top of Tcl - what is it and where can I get a training course to learn about it?Comparing loop commands in TclTrapping errors in Tcl - the safety net that catch providesImages of the new year in Melksham