Overview of C Language Functions

Overview of C Language FunctionsC Language Function Review Overview

In the first chapter, it has been introduced that C source programs are composed of functions. Although the previous chapters’ programs only have one main function main(), practical programs often consist of multiple functions. Functions are the basic modules of C source programs, and specific functions are implemented through the invocation of function modules. Functions in C language are equivalent to subroutines in other high-level languages. C language not only provides a very rich set of library functions (such as Turbo C and MS C, which provide more than three hundred library functions), but also allows users to create their own defined functions. Users can compile their algorithms into relatively independent function modules and then use them through calls.It can be said that all work in C programs is accomplished by various functions, which is why C language is also referred to as a functional language. Due to the modular structure of functions, C language is easy to implement structured program design, making the program’s hierarchical structure clear, facilitating writing, reading, and debugging.In C language, functions can be classified from different perspectives.1. From the perspective of function definition, functions can be divided into library functions and user-defined functions.(1) Library FunctionsProvided by the C system, users do not need to define them and do not need to specify types in the program. They can be directly called in the program as long as the header file containing the function prototype is included at the beginning of the program. Functions such as printf, scanf, getchar, putchar, gets, puts, strcat that have been repeatedly used in the examples of previous chapters belong to this category.(2) User-Defined FunctionsFunctions written by users as needed. For user-defined functions, not only must the function itself be defined in the program, but also a type specification for the called function must be provided in the calling function module before it can be used.2. Functions in C language have the dual functionality of functions and procedures in other languages, from this perspective, functions can also be divided into functions with return values and functions without return values.(1) Functions with Return ValuesThese functions return an execution result to the caller after being called, known as the function return value. Mathematical functions belong to this category. For user-defined functions that need to return function values, the return value type must be explicitly stated in the function definition and function declaration.(2) Functions without Return ValuesThese functions are used to complete specific processing tasks and do not return function values to the caller after execution. Such functions are similar to procedures in other languages. Since functions do not need to return values, users can specify their return type as “void”.3. From the perspective of data transfer between the calling function and the called function, they can be divided into functions without parameters and functions with parameters.(1) Functions without ParametersFunctions that do not carry parameters in their definition, declaration, and invocation. There is no parameter transfer between the calling function and the called function. Such functions are usually used to complete a specified set of functions, and can return or not return function values.(2) Functions with ParametersAlso known as parameterized functions. In the function definition and declaration, there are parameters, known as formal parameters (abbreviated as formals). When calling the function, the actual parameters (abbreviated as actuals) must also be provided. When calling the function, the calling function will pass the values of the actual parameters to the formal parameters for use by the called function.4. C language provides a very rich set of library functions, which can also be classified from the functional perspective as follows.(1) Character Classification FunctionsUsed to classify characters according to ASCII codes: letters, digits, control characters, delimiters, uppercase and lowercase letters, etc.(2) Conversion FunctionsUsed for character or string conversions; converting between character quantities and various numerical quantities (integers, floating points, etc.); converting between uppercase and lowercase.(3) Directory Path FunctionsUsed for file directory and path operations.(4) Diagnostic FunctionsUsed for internal error detection.(5) Graphics FunctionsUsed for screen management and various graphical functions.(6) Input-Output FunctionsUsed to complete input-output functions.(7) Interface FunctionsUsed for interfaces with DOS, BIOS, and hardware.(8) String FunctionsUsed for string operations and processing.(9) Memory Management FunctionsUsed for memory management.(10) Mathematical FunctionsUsed for mathematical function calculations.(11) Date and Time FunctionsUsed for date and time conversion operations.(12) Process Control FunctionsUsed for process management and control.(13) Other FunctionsUsed for various other functionalities.The above categories of functions not only are numerous, but some also require hardware knowledge to use. Therefore, mastering them all requires a long learning process. One should first grasp some of the most basic and commonly used functions before gradually delving deeper. Due to space constraints, this book only introduces a very small portion of library functions, and the remaining parts can be referenced in relevant manuals as needed.It should also be pointed out that in C language, all function definitions, including the main function main, are parallel. That is to say, a function cannot define another function within its body, meaning nesting definitions are not allowed. However, functions are allowed to call each other and also allow nested calls. The calling function is habitually referred to as the calling function. Functions can also call themselves, known as recursive calls. The main function is the main function, it can call other functions but cannot be called by other functions. Therefore, the execution of a C program always starts from the main function, and after completing calls to other functions, it returns to the main function, which finally ends the entire program. A C source program must have, and can only have, one main function main.The general form of function definition1. General form of a function without parametersType Specifier Function Name() { Type Specification Statements }Where the type specifier and function name form the function header. The type specifier indicates the type of the function, which is actually the type of the function’s return value. This type specifier is the same as various specifiers introduced in Chapter 2. The function name is an identifier defined by the user, and there is an empty parenthesis after the function name, which indicates no parameters, but the parentheses cannot be omitted. The content within {} is called the function body. The function body also has type specifications, which are type specifications for the variables used within the function body. In many cases, it is not required for a function without parameters to have a return value; in this case, the function type specifier can be written as void.We can change to a function definition:void Hello(){printf (“Hello,world \n”);}Here, only the main has been changed to Hello as the function name, and everything else remains unchanged. The Hello function is a function without parameters, and when called by other functions, it outputs the string Hello world.2. General form of a function with parametersType Specifier Function Name(Formal Parameter List) Formal Parameter Type Specification { Type Specification Statements }A function with parameters has two additional components compared to a function without parameters: one is the formal parameter list, and the other is the formal parameter type specification. The parameters given in the formal parameter list are called formal parameters, and they can be variables of various types, separated by commas. When calling the function, the calling function will assign actual values to these formal parameters. Since formal parameters are variables, they must also have type specifications. For example, defining a function to find the larger of two numbers can be written as:int max(a,b)int a,b;{if (a>b) return a;else return b;}The first line indicates that the max function is an integer function, and its return value is an integer. The formal parameters are a and b. The second line indicates that a and b are both integer types. The specific values of a and b are passed from the calling function during the call. There are no other variables used in the function body besides the formal parameters, so there are only statements without variable type specifications. This method of definition is called the “traditional format”. This format is not easy for the compiler to check, which can lead to very subtle and difficult-to-track errors. The new ANSI C standard merges the type specifications for formal parameters into the formal parameter list, called the “modern format”.For example, the max function can be defined in modern format as:int max(int a,int b){if(a>b) return a;else return b;}The modern format provides the formal parameters and their types during the function definition and declaration, making it easier to check for errors during compilation, thereby ensuring consistency between the function declaration and definition. Example 1.3 adopts this modern format. In the return statement of the max function, the value of a (or b) is returned to the calling function. At least one return statement should be present in functions with return values. In a C program, the definition of a function can be placed anywhere, either before or after the main function main. For example, in Example 1.3, the max function is defined after the main, but it can also be placed before the main.The modified program is as follows.int max(int a,int b){if(a>b)return a;else return b;}void main(){int max(int a,int b);int x,y,z;printf(“input two numbers:\n”);scanf(“%d%d”,&x,&y);z=max(x,y);printf(“maximum=%d”,z);}Now we can analyze the entire program from the perspectives of function definition, function declaration, and function calling, to further understand the various characteristics of functions. Lines 1 to 5 of the program define the max function. After entering the main function, since we are about to call the max function, we first provide a declaration for it (line 8 of the program). The function definition and function declaration are not the same, which will be discussed separately later. It can be seen that the function declaration is similar to the function header in the function definition, but it ends with a semicolon. Line 12 of the program calls the max function and passes the values of x and y to the formal parameters a and b. The result of executing the max function (either a or b) will be returned to the variable z. Finally, the main function outputs the value of z.The general form of function calling has been mentioned before, in the program, functions are executed through calls to the function body, and this process is similar to subroutine calls in other languages. The general form of function calling in C language is:Function Name(Actual Parameter List) For functions without parameters, there will be no actual parameter list. The parameters in the actual parameter list can be constants, variables, or other constructed type data and expressions, separated by commas. ‘Next of Page In C language, there are several ways to call functions:1. Function ExpressionThe function appears as an item in an expression, participating in the expression’s calculation with its return value. This method requires the function to have a return value. For example: z=max(x,y) is an assignment expression that assigns the return value of max to the variable z. ‘Next of Page2. Function StatementThe general form of function calling plus a semicolon forms a function statement. For example: printf (“%D”,a); scanf (“%d”,&b); are function calls in the form of statements.3. Function Actual ParameterThe function appears as an actual parameter in another function call. In this case, the return value of that function is passed as an actual parameter, so that function must have a return value. For example: printf(“%d”,max(x,y)); means that the return value of the max function is used as an actual parameter for the printf function. In function calling, another issue to note is the order of evaluation. The so-called order of evaluation refers to whether the elements in the actual parameter list are used from left to right or from right to left. The specifications of each system may differ. In section 3.1.3 when introducing the printf function, this was already mentioned, and here it is emphasized again from the perspective of function calling. See Example 5.2 program.void main(){int i=8;printf(“%d\n%d\n%d\n%d\n”,++i,–i,i++,i–);}If evaluated from right to left, the result of Example 5.2 should be:8778If evaluated from left to right, the result should be:9889It should be particularly noted that whether evaluated from left to right or from right to left, the output order remains unchanged, meaning the output order is always the same as the order of actual parameters in the actual parameter list. Since Turbo C is currently defined as right-to-left evaluation, the result is 8, 7, 7, 8. If the above issue is still unclear, trying it out on a machine will clarify it. The parameters of functions and the values of functions.

Overview of C Language Functions

[Source: University of Science and Technology C Language]

Overview of C Language Functions

Leave a Comment