11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

programming as a pr<strong>of</strong>ession 385Here execution jumps from line 20 to line 40. After lines40–60 are executed, the program returns to line 30, whereit ends.Procedures with ParametersThe simple subroutine mechanism has some disadvantages,however. The subroutine gets the information it needs fromthe main part <strong>of</strong> the program implicitly through the globalvariables that have been defined (see variable). If it needsto return information, it does it by changing the value <strong>of</strong> oneor more <strong>of</strong> these global variables. The problem is that manydifferent subroutines may be relying upon the same variables<strong>and</strong> at the same time changing them, leading to unpredictableresults. Modern programming practice thereforegenerally avoids using global variables as much as possible.Most high-level languages today (including Pascal, C/C++, Java, <strong>and</strong> modern versions <strong>of</strong> BASIC) define subprogramsas procedures that pass information through specifiedparameters. For example, a procedure in Pascal mightbe defined as:Procedure PrintChar (CharNum : integer);This procedure has one parameter, an integer that specifiesthe number <strong>of</strong> the character to be printed (see characters<strong>and</strong> strings).The main program can call the procedure by giving itsname <strong>and</strong> an appropriate character number. For example:PrintChar (32);The code within the procedure does not work with theparameter CharNum directly. Rather, it receives a copy that itcan use. Thus, the procedure might include the statements:Writeln (‘Character number: ’, CharNum );Writeln (chr (CharNum));The program will print the character number <strong>and</strong> thenprint the character itself on the next line (for characternumber 32 this will actually be a blank).This typical way <strong>of</strong> using parameters is called passing byvalue. However, it is possible to pass a parameter to a procedure<strong>and</strong> have the parameter itself used rather than workingwith a copy. This is called “passing by reference.” Pascal usesthe var keyword for this purpose, while C passes a pointer tothe variable (see pointers <strong>and</strong> indirection), <strong>and</strong> C++ <strong>and</strong>Java prefix the variable name with an ampers<strong>and</strong> (&). Forexample, suppose one has a C function defined as follows:int ByTwo (int * Val){Val = Val * 2;}In the following statements in the calling program:Int Value, NewValue;Value = 10;NewValue = By Two (Value);NewValue would be set to 20 because the actual variableValue has been multiplied by two inside the ByTwo function.FunctionsA function is a procedure that returns its results as a valuein place <strong>of</strong> the function name in the calling statement. Forexample, a function in C to raise a specified number to aspecified power might be defined like this:int Power (int base, int exp)(C <strong>and</strong> related languages don’t use a keyword like Pascal’sprocedure or function because in C all procedures arefunctions.)This definition says that the Power function takes twointeger parameters, base <strong>and</strong> exp, <strong>and</strong> returns an integervalue.Suppose somewhere in the program there are the followingstatements:Int Base = 8;Int Dimensions = 3;Size = Power (Base, Dimensions);The variable Size will receive the value <strong>of</strong> Power (8, 3) or 512.Although the syntax for using procedures or functionsvaries by language, there are some principles that are generallyapplicable. The type <strong>of</strong> data expected by a procedureshould be carefully defined (see data types). Modern compilersgenerally catch mismatches between the type <strong>of</strong> datain the calling statement <strong>and</strong> what is defined in the proceduredeclaration. Procedures should also be checked forunwanted “side effects,” which they can minimize by notusing global variables.Procedures <strong>and</strong> functions relating to a particular taskare <strong>of</strong>ten grouped into separate files (sometimes called unitsor modules) where they can be compiled <strong>and</strong> linked into aprogram that needs to use them (see library, program).Object-oriented languages such as C++ think <strong>of</strong> proceduresin a somewhat different way from the examplesshown here. While a traditional program sees proceduresas blocks <strong>of</strong> code to be invoked for various purposes, anobject-oriented program sees procedures as “methods” orcapabilities <strong>of</strong> the program’s various objects (see objectorientedprogramming).Further Reading“Introduction to Python: Functions.” Available online. URL: http://www.penzilla.net/tutorials/python/functions/. Accessed August17, 2007.Kernighan, Brian W., <strong>and</strong> Dennis M. Ritchie. The C ProgrammingLanguage. 2nd ed. Englewood Cliffs, N.J.: Prentice Hall, 1988.“Procedures <strong>and</strong> Functions” [in VBScript]. Available online. URL:http://www.functionx.com/vbscript/Lesson06.htm. AccessedAugust 17, 2007.Sebesta, Robert W. Concepts <strong>of</strong> Programming Languages. 8th ed.Boston: Pearson Addison Wesley, 2007.“Subroutines” [in Perl]. Available online. URL: http://www.comp.leeds.ac.uk/Perl/subroutines.html. Accessed August 17, 2007.programming as a pr<strong>of</strong>essionAll computer applications depend upon the ability to directthe machine to perform instructions such as fetching orstoring data, making logical comparisons, or performing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!