Comparison of Pascal and C - Functions, Procedures

Functions, Procedures

Pascal routines that return a value are called functions; routines that don't return a value are called procedures. All routines in C are called functions; C functions that do not return a value are declared with a return type of void.

Pascal procedures are considered equivalent to C "void" functions, and Pascal functions are equivalent to C functions that return a value.

The following two declarations in C:

int f(int x, int y); void k(int q);

are equivalent to the following declarations in Pascal:

function f(x, y: integer): integer; procedure k(q: integer);

Pascal has two different types of parameters: pass-by-value, and pass-by-reference (VAR).

function f(var k: integer): integer; x := f(t);

In C all parameters are passed by value but pass-by-reference can be simulated using pointers. The following segment is similar to the Pascal segment above:

int f(int *k); //function accepts a pointer as parameter x = f(&t);

C allows for functions to accept a variable number of parameters, known as variadic functions.

int f(int a, ...); f(1, 2, 3, 4, 5);

The function f uses a special set of functions that allow it to access each of the parameters in turn. This set of functions was undefined in original C, but was defined in ANSI C.

Additionally Pascal has I/O statements built into the language to handle variable amount of parameters, like Writeln. Pascal allows procedures and functions to be nested. This is convenient to allow variables that are local to a group of procedures, but not global. C does not have this feature and the localization of variables or functions could only be done for a compilation module wherein the variables or functions would have been declared static.

C allows functions to be indirectly invoked through a function pointer. In the following example, the statement (*cmpar)(s1, s2) is equivalent to strcmp(s1, s2):

#include int (*cmpar)(const char *a, const char *b); const char *s1 = "hello"; const char *s2 = "world"; cmpar = &strcmp; b = (*cmpar)(s1, s2);

Though, in this example, it would be wiser to use preprocessor statements to accomplish similar ends (unless cmpar was set based upon run-time conditions, which cannot be handled by the preprocessor).

Pascal also allows functions and procedures to be passed as parameters to functions or procedures:

procedure ShowHex(i: integer); ... end; procedure ShowInt(i: integer); ... end; procedure Demo(procedure Show(i: integer)); var j: integer; begin Show(j) end; ... Demo(ShowHex); Demo(ShowInt); ...

Read more about this topic:  Comparison Of Pascal And C

Famous quotes containing the word procedures:

    Young children learn in a different manner from that of older children and adults, yet we can teach them many things if we adapt our materials and mode of instruction to their level of ability. But we miseducate young children when we assume that their learning abilities are comparable to those of older children and that they can be taught with materials and with the same instructional procedures appropriate to school-age children.
    David Elkind (20th century)