What is a calling function in C.

Beginning back next


Functions are an essential part of C programming and enable structured programming.
  • A function is a named section of C code that performs a specific task. A function can be called from the main program or from other functions.
  • Each function has a unique name, which can be used in other parts of the program to execute the instructions contained in the function.
  • A function is independent in that its task takes place without any external or external influence.
  • A function can take values ​​from the calling program and optionally return a value to the calling program.
Here is an example of a simple function: / * Demonstrates the use of a simple function * / / * Calculates the cube of an integer * / #include / * Function declaration * / long cubus (long x); int main (void) {long input, answer; printf ("\ nEnter a positive integer <1291:"); scanf ("% ld", & input); answer = cubus (input); printf ("\ nThe third power of% ld is% ld. \ n", input, answer); return 0; } / * Function definition * / long cubus (long x) {long x_cubus; x_cubus = x * x * x; return x_cubus; }

Before the start of the main program are the function declarations (function prototype), as a model for the function that appears later in the program. A function declaration contains the name of the function, a list of variables (including their types) that must be passed to it and the type of variable that the function returns if a value is returned. The actual function definition follows later in the program.

Function declaration:

return_type function_name (arg-type name-1, ..., arg-type name-n); Function definition: return_type function_name (arg-type name-1, ..., arg-type name-n) {statements; }

The function declaration provides the compiler with the description of the function that will be defined later in the program.

The actual function is contained in the function definition. The definition contains the code that will be executed later. The first line of the function definition, the function header (function header), is identical to the line of the function declaration except for the semicolon (it would also be possible to omit the variable names in the function declaration). The function head is followed by the function body, which, like the function, is enclosed in curly brackets.

Functions and structured programming

Smaller programming tasks can still be easily implemented within the function, but larger projects with several hundred or thousand instructions are sensibly programmed into smaller parts. When planning a larger program, the so-called "Top-Down Approach" used. In this method, the program is broken down into small sub-units that are linked together in the shape of an upside-down tree. This is illustrated for an address management program in the picture below.

The main function contains very little code, it "mainly" consists of function calls. Complex programming tasks are split up into smaller, simpler parts using functions. These parts are used when the program is created "bottom up" generated. This technology also makes program maintenance easier, since only individual functions need to be changed when changes are made.

Write functions

The first line of a function is the function header, which consists of three parts, each component serving a specific purpose.
Return type Function name Parameter list
type funcname (param1, ...)

The Function return type defines the data type that the function returns to the calling program. This can be any C data type. Functions that do not return any value can also be defined. This is where the data type comes into play.

int func1 (...) / * return value of type int * / double func1 (...) / * return value of type double * / void func1 (...) / * does not return a value * /

For the Function names anything can be taken as long as the C rules of naming are followed. It is a good idea to choose a name that describes the purpose of the function.

The Parameter list specifies which arguments are passed to the function when it is called. For this it is necessary that the type of the arguments of the function is made known. All C data types can also be used here. The information about the data types of the arguments is given in the function header by the parameter list. For each argument that is passed to the function, there must be an entry in the parameter list. The entry specifies the type and name of the parameter.

void xmpl1 (int x, float y, char z)

specifies a function with no return value with three parameters: a type with a name, a type with a name, and a type with a name. If no parameter is passed to the function, the parameter void should appear in the parameter list.

void xmpl2 (void)

The Function declaration, which comes before the start of the main program, differs from the function definition only in that the function declaration is ended with a semicolon. When declaring the function, it is also possible to omit the parameter names in the parameter list.

Immediately after the function head follows the one enclosed in curly brackets Functional body. If a function is called, the program execution begins with the first statement following the opening curly brace and ends with an instruction or with the last statement immediately before the closing curly brace.

Local variable

Variables that are called local variables can be declared within the function. Local means that the variables are only valid within the function and differ from other variables with the same name that have been declared elsewhere in the program. / * locvar.c Demonstrates the use of local variables. * / #include int x = 1, y = 2; void demo (void); int main (void) {printf ("\ nBefore calling demo (), x =% d and y =% d.", x, y); demo(); printf ("\ nAfter calling demo (), x =% d and y =% d.", x, y); return 0; } void demo (void) {/ * Declare and initialize two local variables. * / int x = 88, y = 99; / * Display your values. * / printf ("\ nWithin demo (), x =% d and y =% d.", x, y); }

Passing arguments

The arguments that are passed to the called function are only available in the function as "images" of the original variables. This means that the sizes of the originals cannot be changed in the function. This is known as "call by value". If the values ​​of the original variables are to be changeable, the arguments must be used as references ("call by reference") are passed to the function. This is achieved in C by passing pointers to the original variables. If the originals are to be able to be changed, the names of the variables must not appear in the parameter list, but their addresses must be passed to the function The parameters are to be declared accordingly as pointers in the function declaration. / * swapdemo.c demonstrates call by reference * / #include void swap (int * a, int * b); int main (void) { int x = 13, y = 31; printf ("Before calling swap (): x =% d, y =% d \ n", x, y); swap (& x, & y); printf ("After the Call of swap (): x =% d, y =% d \ n ", x, y); return 0;} void swap (int * a, int * b) {int c = * a; * a = * b; * b = c;}

Transfer of arrays

The transfer of arrays to functions also requires some preparation, since arrays can only be transferred "by reference". This means that there is no information about the size of the transferred field when it is called. In the case of character strings, which are created in the form of character arrays, the end identifier of the character string can serve as length information. For other data types, it makes sense to also include the array size as a parameter. #include void str_output (char s []); void i_output (int a [], int size); int main (void) {char zk [81]; int i_array [] = {2, 4, 8, 16}; printf ("One sentence please:"); gets (zk); str_ausgabe (zk); i_output (i_array, sizeof (i_array) / sizeof (int)); return 0; } void str_output (char s []) {int i; for (i = 0; s [i]! = '\ 0'; i ++) putchar (s [i]); putchar ('\ n'); } void i_output (int a [], int size) {int i; for (i = 0; i Return of value Each function can return a maximum of one value directly to the calling program. This is achieved by means of the instruction. The expression immediately following the statement is returned to the calling program as a return value. The type of the returned value is that which was specified in the function declaration. Attempting to return a value from a function that should not return a value will result in a compiler warning. A value returned from a function can also be ignored in the calling program. If more than one value is to be returned from the function, this must be done using additional parameters that are transferred in the form of pointers.

Use of command line arguments

Arguments from the command line can also be passed to the main program when it is called. This means that when a program is called from the command prompt, information can be passed on to the program. So you can write: C:> progname arg1 arg2 ... argn To be able to use these different arguments from the command line in the main program, the following must be defined: int main (int argc, char * argv []) {/ * Follow here the statements * / return 0; } The first argument supplied by the operating system is an integer that contains the number of command line arguments. The value is always one greater than the number of arguments, since the program name including the path is transferred as the first parameter. The parameter is an array of pointers to strings. The valid subscripts for this array are therefore 0 to. The first parameter from the command line is therefore contained in. The arguments are separated from the command line by spaces. If an argument must contain a space, this argument must be enclosed in quotation marks. C:> progname arg1 has "arg two" as the first argument and the second argument is. The following program shows the use of command line arguments. / * comm_arg.c Use of command line arguments. * / #include int main (int argc, char * argv []) {int count; printf ("Program Name:% s \ n", argv [0]); if (argc> 1) {for (count = 1; count Functions with a variable number of arguments Functions with a variable number of arguments are familiar from library functions such as printf () or scanf (). It is possible to program such functions yourself. Programs that have functions with a variable argument list must include the STDARG.H file.
When declaring a function that has a variable argument list, the fixed parameters must be passed first. At least one fixed argument is required. This is followed by three dots (...) at the end of the parameter list to indicate that none or more additional arguments are passed to the function.
The purpose of the fixed argument (s) is to tell the function the total number and type of arguments. As an example, think of printf (), where the number and type of variables transferred is specified in the format string.
The "Tools" are defined in the STDARG.H file which are necessary for using functions with a variable number of arguments. These are:

, a pointer type,

, a macro used to initialize the argument list,

, a macro to call up the individual arguments one after the other from the variable list.

, a macro that eventually "cleans up" when all arguments have been fetched from the variable list.

The procedure for using these macros is outlined below:

  1. Declare a pointer of type. This pointer is used to access the individual arguments. It is common, but not necessary, to name this variable.
  2. Call the macro and pass the pointer and the name of the last fixed argument. The macro has no return value, but sets the pointer to the first variable in the variable argument list.
  3. To call up the individual arguments, the data type of the next argument is called as a parameter with and. The return value of is the value of the next argument.
  4. If all data has been fetched from the variable list, it is called with as an argument.

Here is an example:

/ * var_arg.c Function with a variable argument list. * / #include #include float average (int num, ...); int main (void) {float x; x = average (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); printf ("\ nThe first mean is% f.", x); x = average (5, 101, 106, 96, 91, 95); printf ("\ nThe second mean is% f.", x); return 0; } float average (int num, ...) {va_list arg_ptr; / * Declare a variable of type va_list. * / int count, total = 0; va_start (arg_ptr, num); / * Initialize the argument pointer. * / / * Retrieve each argument from the variable list. * / for (count = 0; count File I / O