Function (programming)
A function ( english function ) is in the computer science and in various high-level languages , the name of a program construct that allows the program source code structure can be such that portions of the functionality of the program reusable are. The special feature of a function (compared to the similar construct of the procedure ) is that the function returns a result directly and can therefore be used in expressions . The exact designation and details of their characteristics are quite different in different programming languages .
Functions are considered special variants of subroutines .
The program construct 'function' should not be confused with other meanings of the expression ' function ', for example with functions in the sense of organization or systems theory or with function in the sense of task.
Functional programming
The concept of a function in the sense of mathematics is most clearly implemented in functional programming . Here functions represent mapping rules. A function then consists of a series of definitions that describe this rule.
Examples
In Haskell , for example, one would write:
max :: Int -> Int -> Int
max a b | a > b = a
| otherwise = b
This notation is reminiscent of the mathematical definition of the maximum of two numbers.
A functional program consists exclusively of function definitions and has no control structures such as loops. The most important tool for functional programming is therefore recursion .
In functional programming languages, functions are objects that can be worked with like variables. In particular, functions can appear as an argument or return value of another function. One then speaks of functions of a higher order . An example of this is the map
function:
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x : xs) = f x : map f xs
map
takes as an argument of a function a
according to b
where a
and b
any types ( type variable ) and returns a new function that lists a
into lists of b
maps, namely by f
is applied to each element of the list.
Imperative programming
In imperative programming, too, functions are in principle used to calculate a result based on a series of arguments , but by using global variables a function can receive more arguments than can be seen from its argument list, and in this way can also receive more deliver as a result. Changes in the global variables of a function are often referred to as a side effect . The concept of function thus comes close to the procedure in which such "side effects" are in reality the intended main effects (see also effects in computer science ). As a result, programming languages often use misleading terminology: In C , for example, only functions are generally spoken of; Procedures are functions with the return type ; in Modula-2, however, functions are also defined with the keyword .
void
procedure
Direct use of the result
In contrast to procedures, functions return a value that can be used directly. Procedures that do not have a return value can only return results indirectly by changing either reference parameters or global variables.
The Pascal programming language offers an explicit distinction between functions and procedures. A function increment
that increases a number by one can be defined as follows:
function increment(variable: Integer): Integer;
begin
increment := variable + 1;
end;
With a procedure, the calculation result can only be returned indirectly, for example via reference parameters.
procedure increment(variable: Integer, var reference: Integer);
begin
reference := variable + 1;
end;
While the result of a function call can be used directly, the result of a procedure must first be saved in a variable because the variable is passed as a parameter.
program main;
var
variable: Integer;
begin
// Aufruf einer Funktion
writeln(increment(3));
// Aufruf einer Prozedur
increment(3, variable);
writeln(variable);
end;
Examples
Pascal
Pascal distinguishes between functions and procedures:
- Functions are
function
declared with the keyword and provide a return value with a defined type. Function calls are inside expressions. The return value is determined by a (pseudo) assignment to the function name. As a side effect, however, functions can change the state of the program by giving global variables new values. -
Procedures are
procedure
declared with the keyword and have no defined return value. Their effect can therefore only be seen in the changes to global variables.
Example of a function declaration in Pascal:
function summe(a: Integer; b: Integer): Integer;
begin
summe := a + b;
end;
Example of a function call in Pascal:
ergebnis := summe(1, 2);
In Pascal, functions and procedures can be nested, unlike in C. This means that they can contain further functions and procedures.
Arguments are usually considered value parameter passed (call / pass by value) . The value of a variable passed as an argument to a function (or procedure) is not changed by its execution. However, it is also possible to use the keyword to specify var
the transfer as reference (call by reference). This means that the value of a variable is not transferred, but its address.
C.
Arguments are generally always the value parameter passed (call / pass by value) . If a transferred variable is to be changed, its address is transferred, i.e. a pointer . The return value is determined by an return
instruction. Functions in C cannot be nested. However, some C compilers deliver non-standardized extensions that allow nesting.
Example of a function agreement in C :
int summe(int a, int b) {
return a + b;
}
Example of a function call in C:
ergebnis = summe(1, 2);
Automation technology: PLC programming
In automation technology, functions (FCs) and function blocks (FBs) are used as library- compatible blocks for PLC programming .
literature
- Siegfried Grohmann, Dirk Papendieck, OStR Peter Westphal-Nagel: Automation technology with Simatic S7. Programming projects for professional education and training. 3rd, revised and updated edition, Elektronik-Praktiker-Verlag (EPV), Duderstadt 2009, ISBN 978-3-936318-75-3 , 160 pages.