|
|
|
|
|
Programming Functions/ ProceduresSubroutines and set constructs that use the program language to break and name code that returns a value.In this article, we shall be discussing functions (procedures, subroutines, methods etc.) – facilities offered by a programming language.
IntroductionAll programming languages have a set of constructs which allow the programmer to break up their code into smaller, more manageable, chunks which can be called by name and which may or may not return a value. The purpose of these blocks is to decrease the complexity of a piece of code, and allow the programmer to reuse specific functions, rather than constantly repeating the same block of code performing a specific operation. For example, a language that has no built-in command for displaying text will need to provide a function to interface with the operating system to display a text string. If there was no function for this, the programmer would need to copy the same code over and over each time they wanted to print something on the screen. If the operating system changed in some way, they would have to change many pieces of code, unless a function was used, in which case they could just update the single block that is called each time output is required. SubroutinesThe simplest kind of user defined code block is known as a subroutine. In BASIC programming, where each line of code is numbered, statements such as GOSUB 100 can be encountered, followed by a block of code to be executed, and a RETURN statement, which ends execution of the user defined code block, and returns control to the line following the GOSUB statement. More advanced subroutine support includes the use of labels, rather than line numbers. Label support in programming languages makes them easier to read and maintain, since GOSUB ReadALine makes more sense than GOSUB 1042. Procedures and FunctionsSince a subroutine does not allow easy communication (beyond the use of global variables - see The Variables Tutorial for more information) with the rest of the program, modern programming languages use procedures and functions to increase the flexibility of the subroutine principle. Different programming languages approach user defined functions in different ways, but they all share the possibility for the programmer to define the name, the information to be passed to (and returned from) the function, as well as the code that is executed between the start and end block delimiters. Procedural languages tend to make the distinction between a user defined named code block that returns a value, often called a FUNCTION, and one that does not, denoted by the work PROCEDURE. MethodsA method is a special kind of user defined code block, which is used in Object Oriented techniques. It can be seen as a function belonging to a self contained object, that is entirely opaque, and which is only ever called when the programmer wishes to interface with the object that defines it. ConclusionThere are other aspects to flow control which allow conditional execution of code (see the Loops tutorial and the (see the Condition Testing tutorial). Mastering all three types (including this one) is usually a pre-requisite for being able to use a programming language effectively. Navigation LinksMailing ListStay informed - sign up to the mailing list!
The copyright of the article Programming Functions/ Procedures in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Programming Functions/ Procedures in print or online must be granted by the author in writing.
|
|
|
|