Types of Functions
Types of Functions
The following examples illustrate various methods of passing values to functions. Except for the function
"strcopy()", these are not working functions (code has been omitted).
Contents
A FUNCTION WHICH PASSES NO VALUE AND RETURNS NO VALUE . . . . . . 1
A FUNCTION WHICH PASSES TWO FLOATS AND RETURNS A FLOAT . . . . . 1
A FUNCTION WHICH PASSES AN INTEGER ARRAY AND RETURNS AN INTEGER . 2
A FUNCTION WHICH PASSES VARIABLES BY REFERENCE USING ADDRESSES . 2
A FUNCTION WHICH PASSES A STRING BY REFERENCE . . . . . . . . . 3
A FUNCTION WHICH PASSES A STRUCTURE BY NAME . . . . . . . . . . 4
A FUNCTION WHICH PASSES A STRUCTURE BY REFERENCE USING A POINTER . 5
A FUNCTION WHICH PASSES A STRUCTURE ARRAY . . . . . . . . . . . 6
A FUNCTION WHICH PASSES A FILE NAME . . . . . . . . . . . . . . 7
A function may be declared (function prototype) globally or within the calling function:
The variables used in the function need not and should not have the same names as those
passed to the function:
An alternate method would be to pass by reference using a pointer. In this example the last
argument is an integer telling the function how many elements are in the array:
The variables used in the function need not and should not have the same names as those
passed to the function:
There is no way that I can find of returning a string from a function. However, if the address
of the string is passed, then the function can operate on the string. This example is a
working function which takes the string referenced by the second argument, removes the
carriage return from the end of it and "returns" it by assignment to the first argument. (This
is used for a string which has been retrieved from a text file using the fgets() function):
The calling function must have declared two appropriate character arrays.
char Name1[25];
char Name2{25];
FUNCTION CALL strcopy(Name2,Name1);
FUNCTION HEADER void strcopy(char Str2[], char Str1[])
{
DECLARE A VARIABLE int Cnt = 0;
while (Str1[Cnt] != '\n')
{
Str2[Cnt] = Str1[Cnt];
++Cnt;
}
Nothing is returned, but "Str2" is the new version of the original "Name1" and is available
in the calling function as "Name2".
The function prototype may be declared globally or within the calling function. Here
"class_list" is the type of structure from the structure prototype (declared globally), not the
specific structure itself:
A single structure of type "class_list" is created in the calling function (if not globally) and
named "load":
The function prototype may be declared globally or within the calling function. Here
"class_list" is the type of structure from the structure prototype (declared globally), not the
specific structure itself. The * indicates that a pointer to the structure will be passed:
A single structure of type "class_list" is created in the calling function (if not globally) and
named "load":
The function prototype may be declared globally or within the calling function. Here "c_list"
is the type of structure from the structure prototype (declared globally), not the specific
structure itself. The * indicates that a pointer to the structure will be passed:
A structure array of type "c_list" is created in the calling function and assigned to pointer
"Ptr" and memory is allocated. "Elements" is the number of elements in the array: