Function encapsulation of C language for the first time

Function overview

The basic unit of C program is function. Function contains the executable code of the program.

The entry and exit of each C program are in the main function. When writing a program, not all the contents are put in the main function main. In order to facilitate the planning, organization, writing and debugging, the general practice is to divide a program into several program modules, and each program module completes part of the function. In this way, different program modules can be completed by different people, which can improve the efficiency of software development.

In other words, the main function can call other functions, and other functions can also call each other. Call other functions in the main function. After execution, these functions are returned to the main function. These called functions are usually called lower level functions. When a function call occurs, the called function is executed immediately, while the caller enters a waiting state until the called function is executed. Functions can have arguments and return values.

Function call diagram of a program

[example 9.1] calling other functions in the main function.

#include<stdio.h>
void Move();	/*Declaration function*/
void Build();	/*Declare build function*/
void Paint();	/*Declaration whitewash function*/
int main()
{
	Move();	                /*Execute the transport function*/
	Build();		/*Execute build function*/
	Paint();	        /*Execute whitewash function*/
	return 0;	        /*End of procedure*/
}
/*Perform the handling function  */
void Move()
{
	printf("This Function can move material\n");
}
/*Perform build function  */
void Build()
{
	printf("This Function can build a building\n");
}
/*	Perform painting function  */
void Paint()
{
	printf("This Function can paint cloth\n");
}

Definition of function

When writing a function in a program, the definition of a function is to let the compiler know the function. The defined function includes function head and function body.

Function header

It is divided into three parts

Return value type. The return value can be a C data type.

Function name. The function name is also the function identifier. The function name must be unique in the program. Because it is an identifier, the function name should also follow the identifier naming rules.
Parameter table. The parameter table can have no variables or multiple variables, and the actual parameters will be copied to these variables when the function is called.

Function body

Function body includes declaration of local variable and executable code of function.

The form of function definition

Library functions of C language can be called directly when programming, such as printf output function. The user-defined function must be defined by the user to complete the function specific functions in its function definition, so that it can be called by other functions.

The definition of a function is divided into two parts, function head and function body.

The syntax format of function definition is as follows:   The code to define a function is as follows:

Return value type function name(parameter list) 

{

	Function body(Function to achieve a specific function of the process);

}
int AddTwoNumber(int iNum1,int iNum2)	/*Function header*/

{			
	                        /*The function body part realizes the function function*/
	int result;		/*Define integer variables*/
	result = iNum1+iNum2;	/*Carry out the addition operation*/
	return result;		/*Return to operation result, end*/
}

The process of defining function is analyzed by code

Function header

Function header is used to mark the beginning of a function code, which is the entrance of a function. The function header is divided into three parts

Return value type.
Function name

parameter list

Function body

The function body is located below the function head, surrounded by a pair of braces, which determine the scope of the function body. Function to achieve specific functions, are in the body of this part of the function through the code statement. Finally, the result of the implementation is returned through the return statement. In the above code, the function of AddTwoNumber is to add two integers. Therefore, an integer is defined to save the calculation result of the addition. Then, the parameter passed in is used to add, and the result is saved in the result variable. Finally, the function returns the result. Through the operation of these statements, the specific functions of the function are realized.

Function classification

Now we have learned what kind of syntax format should be used to define a function. There are several special situations when defining a function:

Nonparametric function

Nonparametric functions are functions without parameters. The syntax of nonparametric function is as follows:

Return value type function name()

{

    Function body

}

Null function

As the name suggests, an empty function is a function without any content and has no practical effect. Since empty functions have no practical function, why do they exist? The reason is that the position of the empty function is to place a function, but the function has not yet been programmed. Use the empty function to occupy a position first, and then use a programmed function to replace it.

The form of empty function is as follows:

Type specifier function name()

{

}

Definition and declaration

When writing a function in a program, you need to declare the function first, and then define the function. Function declaration is to let the compiler know the name, parameters, return value type and other information of the function. The definition of a function is to let the compiler know the function.

The declaration format of a function consists of four parts: function return value type, function name, parameter list and semicolon.

return type   Function name (parameter list);

Note that there should be a semicolon at the end of the declaration As the end of the statement. For example, the code to declare a function is as follows:

int ShowNumber(int iNumber);

[example 9.2] definition and declaration of function.

#include<stdio.h>
/*Declaration of function*/
void ShowNumber(int iNumber);
int main()
{
	int   iShowNumber;	/*Define integer variables*/
	printf("What Number do you wanna show?\n");/*Output prompt information*/
	scanf("%d",&iShowNumber);	/*Enter an integer*/
	ShowNumber(iShowNumber);	/*Call function*/
	return 0;	/*End of procedure*/
}
/*Definition of function*/
void ShowNumber(int iNumber)		

{			
	printf("You wanna to show the Number is:%d\n",iNumber);/*Output integer*/
}

Return statement

Return from function

[example 9.3] return from function.

int Function()	/*Define function*/
{
	printf("this step is in the function\n");/*Output prompt information*/
	/*End of function*/
}
#include<stdio.h>
int Function();		/*Declaration function*/
int main()
{
	printf("this step is before the Function\n");	/*Output prompt information*/
	Function();	/*Call function*/
	printf("this step is end of the Function\n");	/*Output prompt information*/
	return 0;
}

Return value

Usually, the caller wants to call other functions to get a certain value, which is the return value of the function. For example, the following code:

int Minus(int iNumber1,int iNumber2)
{
	int iResult;	/*Define an integer variable to store the returned result*/

	iResult=iNumber1-iNumber2;	/*The results are obtained by subtraction*/
	return result;	/*return Statement returns the result of the calculation*/
}
int main()
{
	int iResult;	/*Define an integer variable*/
	iResult=Minus(9,4);/*Subtract 9-4 and assign the result to the variable iResult*/
	return 0;	/*End of procedure*/
}

In the above code, we can see that we first define a Minus function Minus. In the main function main, we call Minus to assign the calculated Minus result to the variable iResult defined in main.

[example 9.4] return value type and return value type

#include<stdio.h>
char ShowChar();	/*Declaration of function*/
int main()
{
	char cResult;
	cResult=ShowChar();	/*Subtract 9-4 and assign the result to the variable iResult*/
	printf("%c\n",cResult);	/*Output the returned results*/
	return 0;		/*End of procedure*/

}
char ShowChar()
{
	int iNumber;	/*Define integer variables*/
	printf("please input a number:\n");	/*Output prompt information*/
	scanf("%d",&iNumber);/*Enter an integer variable*/
	return iNumber;	/*Returns an integer*/
}

Function parameters

Formal parameters and actual parameters

When using functions, formal parameters and actual parameters are often used. Both are called parameters, so what's the relationship between them? What is the difference between the two? What is the role of the two parameters? Next, through the name and function of formal parameters and actual parameters to understand, and then through a metaphor and an example to understand.

Understanding by name

Formal parameters, understood by name, are formal parameters.

The actual parameter, understood by name, is the actual parameter.

Understanding through function

Formal parameter: when defining a function, the variable name in brackets after the function name is "formal parameter". The values passed to the function are copied into these formal parameters before the function is called.

Actual parameters: when a function is called, that is, when a function is actually used, the parameters in brackets after the function name are "actual parameters". The parameters provided to a function by the caller of a function are called actual parameters. The actual parameter is the result of the expression evaluation and is copied to the formal parameter of the function.

Understand formal parameters and actual parameters through a metaphor

The mother took a bag of milk, poured it into an empty bottle, and then fed the baby milk. The function is equivalent to the baby drinking milk with a milk bottle. The actual parameter is equivalent to a bag of milk brought by the mother, while the empty milk bottle is equivalent to a formal parameter. The action of putting milk into the bottle is equivalent to passing the actual parameter to the formal parameter. Using the bottle filled with milk is equivalent to the process of using the parameter to operate the function.

[example 9.5] figurative realization of formal parameters and actual parameters.

#include<stdio.h>
void DrinkMilk(char* cBottle);	/*Declaration function*/
int main()
{
	char cPoke[]="";			/*Define character array variables*/
	printf("Mother wanna give the baby:");	/*Output information prompt*/
	scanf("%s",&cPoke);	/*Input string*/
	DrinkMilk(cPoke);		/*Passing actual parameters to formal parameters*/
	return 0;		/*End of procedure*/
}
/*The act of drinking milk*/
void DrinkMilk(char* cBottle)	/*cBottle Is a formal parameter*/

{
	printf("The Baby drink the %s\n",cBottle);	/*Output prompt, drink milk action*/
}

Array as function parameter

This section discusses the special case of arrays being passed as arguments to functions. The array is passed as a function parameter, which is different from the standard parameter passing method of assignment call.

When an array is used as an argument of a function, only the address of the array is passed, instead of assigning the entire array to the function. When a function is called with an array name as an argument, a pointer to the first element of the array is passed to the function.

When declaring function parameters, they must have the same type. According to this, we will explain in detail the various cases of using arrays as function parameters.

Using array elements as function parameters

Because the argument can be in the form of an expression and the array element can be a part of an expression, the array element can be used as the argument of a function, which is the same as using a variable as the argument of a function.

[example 9.6] array elements are used as function parameters.

#include<stdio.h>
void ShowMember(int iMember);	/*Declaration function*/
int main()
{
	int iCount[10];	/*Defines an array of integers*/
	int i;		/*Define integer variables for loops*/
	for(i=0;i<10;i++)	/*Perform the assignment loop*/
	{
		iCount[i]=i;	/*Assign values to elements in an array*/
	}
        for(i=0;i<10;i++)		/*Cycle operation*/
	{
		ShowMember(iCount[i]);/*Perform output function operations*/
	}
	return 0;
}
void ShowMember(int iMember)	/*Function definition*/
{
	printf("Show the member is%d\n",iMember);/*output data*/
}

Array name as function parameter

You can use the array name as a function parameter. In this case, the array name is used for all the real parameters.

[example 9.7] the array name is used as the function parameter.

#include<stdio.h>
void  Evaluate(int iArrayName[10]);/*Declare assignment function*/
void  Display(int iArrayName[10]);	/*Declare display function*/
int main()
{
	int iArray[10];	/*Define an integer array with 10 elements*/
	Evaluate(iArray[10]);/*Call the function to perform the assignment operation, and take the array name as the parameter*/
	Display(iArray[10]);/*Call the function to perform the assignment operation, and take the array name as the parameter*/
	return 0;
}
void  Display(int iArrayName[10])
{
	int i;		/*Define integer variables*/
	for(i=0;i<10;i++)	/*The statement that executes the loop*/
{	/*Performing output operations in loop statements*/
		printf("the member number is %d\n",iArrayName[i]);
	}
}
/*	Assigning values to array elements	*/
void  Evaluate(int iArrayName[10])
{
	int i;	/*Define integer variables*/
	for(i=0;i<10;i++)	*Execute loop statement*/
	{	/*Performing assignment operations in loop statements*/
		iArrayName[i]=i;
	}
}

Variable length array as function parameter

You can use the array name as a function parameter. In this case, the array name is used for all the real parameters.

[example 9.8] variable length array as function parameter.

#include<stdio.h>
void  Evaluate(int iArrayName[]);	/*Declare the function, and the parameter is a variable length array*/
void  Display(int iArrayName[]);	/*Declare the function, and the parameter is a variable length array*/
int main()
{
	int iArray[10];/*Define an integer array with 10 elements*/
	Evaluate(iArray[10]);/*Call the function to perform the assignment operation, and take the array name as the parameter*/
	Display(iArray[10]);/*Call the function to perform the assignment operation, and take the array name as the parameter*/
	return 0;
}
void  Display(int iArrayName[])	/*Define the function, the parameter is variable length array*/
{
	int i;	/*Define integer variables*/
	for(i=0;i<10;i++)	/*The statement that executes the loop*/
	{	/*Performing output operations in loop statements*/
		printf("the member number is %d\n",iArrayName[i]);
	}
}
/*Assigning values to array elements*/

void  Evaluate(int iArrayName[])	/*Define the function, the parameter is variable length array*/
{
	int i;	/*Define integer variables*/
	for(i=0;i<10;i++)	/*Execute loop statement*/
	{	/*Performing assignment operations in loop statements*/
		iArrayName[i]=i;
	}
}

Using pointers as function parameters

The last way is to declare a function parameter as a pointer.

[example 9.9] pointer as function parameter

#include<stdio.h>
void  Evaluate(int* pPoint);/*Declare the function, and the parameter is a variable length array*/
void  Display(int* pPoint);/*Declare the function, and the parameter is a variable length array*/
int main()
{
	int iArray[10];	/*Define an integer array with 10 elements*/
	Evaluate(iArray);	/*Call the function to perform the assignment operation, and take the array name as the parameter*/
	Display(iArray);	/*Call the function to perform the assignment operation, and take the array name as the parameter*/
	return 0;
}
void  Display(int* pPoint)/*Define the function, the parameter is variable length array*/
{
	int i;	/*Define integer variables*/
	for(i=0;i<10;i++)	/*The statement that executes the loop*/

	{	/*Performing output operations in loop statements*/
		printf("the member number is %d\n",pPoint[i]);
	}}
void  Evaluate(int* pPoint)	/*Define the function, the parameter is variable length array*/
{
	int i;	/*Define integer variables*/
	for(i=0;i<10;i++)	/*Execute loop statement*/
	{	/*Performing assignment operations in loop statements*/
		pPoint[i]=i;
	}}

Parameters of main

In the previous introduction of function definition, we mentioned the main function main when explaining function body. Based on this, we will introduce the parameters of main function.

When running a program, it is sometimes necessary to pass the necessary parameters to the main function. The formal parameters of main function are as follows:

main (int argc, char* argv[] )   

Two special internal parameters argc and argv are used to receive command-line arguments, which are only available to the main function main.

argc parameter

argc parameter holds the number of parameters in the command line, which is an integer variable. The value of this parameter is at least 1, because at least the program name is the first argument.

argv parameter

The argv parameter is a pointer to an array of character pointers in which each element points to a command line argument. All command line arguments are strings, and any number must be converted from the program to the appropriate format.

[example 9.10] use of main parameters.

#include<stdio.h>

int main(int argc,char* argv[])

{
	printf("%s\n",argv[0]);/*Location of output program*/

	return 0;	/*End of procedure*/

}

Function call

Function call mode

There is not only one way to use a tool, but also the function call. There are three ways to call functions, including function statement call, function expression call and function parameter call. The following three situations are introduced.

Function statement call

To call a function as a statement is called a function statement call. Function statement call is the most commonly used way to call functions, as follows:

Display();   /* Display a message*/

The function displays a message inside the function. At this time, the function is not required to have a return value, only to complete certain operations.

[example 9.11] function statement call. This example uses the statement to call the function, through calling the function to complete the function of displaying a message, and then observe the use of the function statement call.

#include<stdio.h>
void Display()	/*Define function*/
{
	printf("Just show this message.");/*Realize the function of displaying a message*/
}
int main()
{
	Display();	/*Function statement call*/

	return 0;	/*End of procedure*/

}

Function expression call

When a function appears in an expression, the function is required to bring back a certain value, which is used as the operation of the expression. As shown in the following code:

iResult=iNum3*AddTwoNum(3,5);   /* Function in expression*/

As you can see, the function AddTwoNum in this statement is to add two numbers. In the expression, AddTwoNum multiplies the added result with the iNum3 variable and assigns the result to the iResult variable.

[example 9.12] function expression call. In this example, a function is defined. The function of the function is to add the computation and call the function in the expression, so that the return value of the function will take part in the operation and get the new result.

#include<stdio.h>
/*Declare the function and add the function*/
int AddTwoNum(int iNum1, int iNum2);
int main()
{
	int iResult;	/*Define variables to store calculation results*/
	int iNum3=10;	/*Define the variable and assign it to 10*/
	iResult=iNum3*AddTwoNum(3,5);	/*Calling function AddTwoNum in expression*/
	printf("The result is : %d\n",iResult);	/*Output the calculation results*/
	return 0;/*End of procedure*/
}
int AddTwoNum(int iNum1, int iNum2)/*Define function*/
{
	int iTempResult;		/*Define integer variables*/
	iTempResult=iNum1+iNum2;/*Add and assign the result to iTempResult*/
	return iTempResult;	/*Return calculation results*/
}

Function parameter call

A function call is used as an argument to a function, so the function return value is passed to the function as an argument.

When a function appears in an expression, it is required to bring back a certain value, which is used to participate in the operation of the expression. The code is as follows:

iResult=AddTwoNum(10,AddTwoNum(3,5));/* Function in parameter*/

In this statement, the function AddTwoNum is used to add two numbers. AddTwoNum takes the result of the addition as the parameter of the function and continues the calculation.

[example 9.13] function parameter call. This example is modified on the basis of the previous program to carry out the operation of continuous addition.

#include<stdio.h>
/*Declare the function and add the function*/
int AddTwoNum(int iNum1, int iNum2);
int main()
{
	int iResult;/*Define variables to store calculation results*/
	iResult=AddTwoNum(10,AddTwoNum(3,5));	/*Calling function AddTwoNum in parameters*/
	printf("The result is : %d\n",iResult);/*Output the calculation results*/
	return 0;	/*End of procedure*/
}
int AddTwoNum(int iNum1, int iNum2)/*Define function*/
{
	int iTempResult;	/*Define integer variables*/
	iTempResult=iNum1+iNum2;	/*Add and assign the result to iTempResult*/
	return iTempResult;	/*Return calculation results*/
}

Nested Call

In C language, the definitions of functions are parallel and independent, that is to say, when defining a function, a function body cannot contain another function defined, which is different from PASCAL Language (PASCAL allows to include the definition of another function in the function body when defining a function, and this form is called nested definition). For example, the following code is wrong:

int main()
{
	void  Display()	/*Wrong!!! A function cannot be defined within a function*/

	{

		printf("I want to show the Nesting function");

	}
	return 0;
}

Recursive call

C language functions support recursion, that is, each function can call itself directly or indirectly. The so-called indirect call refers to calling itself in the lower level of recursive function call. The recursive relationship is shown in the figure.

The reason why recursion can be realized is that each execution process of the function has its own copy of formal parameters and local variables in the stack, which has no relationship with other execution processes of the function.

[example 9.15] recursive call of function. In this example, define a string array, assign a series of names to the array, and finally display the list in reverse order through the call of recursive function.

#include<stdio.h>
void DisplayNames(char** cNameArray);	/*Declaration function*/
char* cNames[]=		/*Define string array*/
{
	"Aaron",		/*Assign a value to a string*/
	"Jim",
	"Charles",
	"Sam",
	"Ken",
	"end"			/*Set end flag*/
};
int main()
{
	DisplayNames(cNames);	/*Call recursive function*/
	return 0;
}
void DisplayNames(char** cNameArray)
{
	if(*cNameArray=="end")/*Judgment end flag*/
	{
		return ;	/*Function end return*/
	}
	else
	{
		DisplayNames(cNameArray+1);/*Call recursive function*/
		printf("%s\n",*cNameArray);/*Output string*/
	}}

Internal and external functions

Internal function

Define a function. If you want this function to be used only by the source file where it is located, then such a function is called an internal function. Internal functions are also called static functions. If there are internal functions with the same name in different source files, these functions will not interfere with each other.

When defining an internal function, the keyword static should be added before the function return value and function name

static   return type   Function name   ( Parameter list)

For example, define an internal function whose function is to perform addition operation and whose return value is int type. The code is as follows:

static int Add(int iNum1,int iNum2)

Add the keyword static before the return value type int of the function to modify the original function into an internal function.

[example 9.16] the use of internal functions. In this function, internal functions are used to assign values to strings through a function, and then output and display strings through a function.

#include<stdio.h>
static char* GetString(char* pString)	/*Define assignment function*/
{
	return pString;	/*Return character*/
}
static void ShowString(char* pString)	/*Define output function*/
{
	printf("%s\n",pString);/*display string*/
}
int main()
{
	char* pMyString;	/*Define string variables*/
	pMyString=GetString("Hello!");	/*Call the function to assign a value to the string*/
	ShowString(pMyString);	/*display string*/
	return 0;
}

External function

The opposite of internal function is external function, which can be called by other source files. Define the external function and modify it with the keyword extern. When you use an external function, you should first use extern to declare that the function you are using is an external function.

For example, the function header can be written in the following form:

extern int Add(int iNum1,int iNum2);

In this way, the function Add can be called by other source files for addition.

[example 9.17] the use of external functions. In this example, the external function is used to complete the same function as the internal function in the above example, except that the function used is not included in the same source file.

/*	ExternFun.c*/
#include<stdio.h>
extern char* GetString(char* pString);/*Declare external functions*/
extern void ShowString(char* pString);/*Declare external functions*/
int main()
{
	char* pMyString;	/*Define string variables*/
	pMyString=GetString("Hello!");	/*Call the function to assign a value to the string*/
	ShowString(pMyString);	/*display string*/
	return 0;
}
/*ExternFun1.c*/
extern char* GetString(char* pString)
{
	return pString;	/*Return character*/
}

/*	ExternFun2.c	*/
extern void ShowString(char* pString)
{
	printf("%s\n",pString);/*display string*/
}

Local variable and global variable

local variable

Variables defined within a function are local variables. Most of the variables in the above examples are only local variables, which are declared inside the function and cannot be used by other functions. The formal parameter of a function is also a local variable, and its scope is limited to all statement blocks within the function.

The scope of local variables in different cases.

[example 9.18] scope of local variable. This example defines some variables in different positions, and assigns values to them to indicate the position of the variables. Finally, the output shows the value of the variables, and the scope of action of local variables is observed through the output information.

#include<stdio.h>
int main()
{
	int iNumber1=1;	/*iNumber1 The scope of is in the whole main function*/
	if(iNumber1>0)
	{
		int iNumber2=2;/*iNumber2 The scope of is in the if statement block*/
		if(iNumber2>0)
		{
			int iNumber3=3;/*iNumber3 The scope of is in the if statement block*/
			/*Output all three functions in this scope*/
			printf("All three number are in scope here %d  %d  %d\n",
				iNumber1,iNumber2,iNumber3);
		}
	}
	return 0;
}

[example 9.19] shielding effect of local variables. In this example, three variables with the same name are defined in different statement blocks, and the shielding effect of local variables is demonstrated by outputting variable values.

#include<stdio.h>
int main()		/*In main function*/
{
	int iNumber1=1;	/*Define the location in the first iNUmber1*/
	printf("%d\n",iNumber1);/*Output variable value*/
	if(iNumber1>0)
	{
		int iNumber1=2;/*Define the location in the second iNUmber1*/
		printf("%d\n",iNumber1);/*Output variable value*/
		if(iNumber1>0)
{

			int iNumber1=3;	/*Define the location in the third iNUmber1*/
			printf("%d\n",iNumber1);/*Output variable value*/
		}
		printf("%d\n",iNumber1);/*Output variable value*/
	}
	printf("%d\n",iNumber1);	/*Output variable value*/
	return 0;	
}

global variable

The compilation unit of the program is the source file. Through the introduction above, we can understand that the variables defined in the function are called local variables. If a variable is declared outside all functions, it is a global variable. As the name suggests, global variables are variables that can be accessed anywhere in the program.

The function of defining global variables is to increase the channel of data connection between functions. Because all functions in the same file can refer to the value of global variable, if the value of global variable is changed in one function, other functions will be affected, which means that there is a direct transfer channel between functions.

For example, there is a national chain store organization, and the prices used by the stores are unified across the country. There are many such chain stores all over the country. When making price adjustment, we should ensure that the price of each chain store is the same. The global variable is like the price to be set, and the function is like every chain store. When the global variable is modified, the variable used in the function is changed.

Function application

In order to make users write programs quickly, the compiler system will provide some library functions, and the library functions provided by different compiler systems may not be exactly the same. It is possible that the function names are the same but the functions implemented are different. It is also possible to realize the unified function but the function names are different. The standard library functions recommended by ANSI C standard include the library functions provided by most current C compiler systems. Here are some common library functions.

In the program, we often use some mathematical operations or formulas. Here we first introduce the common functions related to mathematics.

abs function

The function is to find the absolute value of an integer. The function is defined as follows:

int abs(int i);

For example, to find the absolute value of a negative number:

int iAbsoluteNumber;   /* Define integers*/

int iNumber = -12;   /* Define an integer and assign it - 12*/

iAbsoluteNumber=abs(iNumber);   /* Assign the absolute value of inount to the iabsolutenumber variable*/

labs function

The function is to find the absolute value of a long integer. The function is defined as follows:

long labs(long n);

For example, to find the absolute value of a long integer:

long lResult;   /* Define long integers*/

long lNumber = -1234567890L;   /* Define a long integer and assign it - 1234567890*/

lResult= labs(lNumber);   /* Assign the absolute value of lnumber to the iResult variable*/

fabs function

The function returns the absolute value of a floating-point number. The function is defined as follows:

double fabs(double x);

For example, to find the absolute value of a real type:

double fFloatResult;   /* Define real variables*/

double fNumber = -1234.0;   /* Define a real variable and assign it - 1234.0*/

fFloatResult= fabs(fNumber);   /* Assign the absolute value of fnumber to the fResult variable*/

[example 2.21] use of math library function. In this example, the three library functions described above are put together, and the functions are observed by calling the functions.

#include<stdio.h>
#include<math.h> 	/* Include header file math. H*/
int main()
{
	int iAbsoluteNumber;/*Define integers*/
	int iNumber = -12;	/*Define an integer and assign it - 12*/
	long lResult;	/*Define long integers*/
	long lNumber = -1234567890L; /*Define a long integer and assign it - 1234567890*/
	double fFloatResult;/*Define floating point type*/
	double fNumber = -123.1; 	/*Define floating-point type and assign it - 1234.0*/
        iAbsoluteNumber=abs(iNumber);/*Assign the absolute value of inount to the iAbsoluteNumber variable*/
	lResult= labs(lNumber);	/*Assign the absolute value of lNumber to the iResult variable*/
	fFloatResult= fabs(fNumber);	/*Assign the absolute value of fNumber to the fResult variable*/
        /*Output the original number, and then output the absolute value*/
	printf("the original number is: %d, the absolute is:                %d\n",iNumber,iAbsoluteNumber);
	printf("the original number is: %ld, the absolute is: %ld\n",lNumber,lResult);
	printf("the original number is: %lf, the absolute is: %lf\n",fNumber,fFloatResult);
	return 0;
}

sin function

The function is sine function. The function is defined as follows:

double sin(double x);

For example, the method of calculating sine value:

double fResultSin;   /* Define real variables*/

double fXsin = 0.5;   /* Define real variables and assign values*/

fResultSin = sin(fXsin);   /* Use sine function*/

 

cos function

The function is: cosine function. The function is defined as follows:

double cos(double x);

For example, the method of calculating cosine value:

double fResultCos;   /* Define real variables*/

double fXcos = 0.5;   /* Define a real variable and assign it a value of 0.5*/

fResultCos = cos(fXcos);   /* Call cosine function*/

tan function

The function is tangent function. The function is defined as follows:

double tan(double x);

For example, the method of finding tangent value:

double fResultTan;   /* Define real variables*/

double fXtan = 0.5;   /* Define a real variable and assign it a value of 0.5*/

fResultTan = tan(fXtan);   /* Call tangent function*/

isalpha function

The function detects letters and returns non-zero if the parameter (ch) is a letter in the alphabet (upper or lower case). Include the header file ctype.h. The function is defined as follows:

int isalpha( int ch );

For example, the method of judging whether the input character is a letter:

char c;   /* Define character variables*/

scanf( "%c", &c );   /* Input character*/

isalpha(c);   /* Call isalpha function to judge the input character*/

 

isdigit function

The function detects a number. If ch is a number, the function returns a non-zero value, otherwise it returns zero. Include the header file ctype.h. The function is defined as follows:

int isdigit( int ch );

For example, the method of judging whether the input character is a number:

char c;   /* Define character variables*/

scanf( "%c", &c );   /* Input character*/

isdigit(c);   /* Call isDigit function to judge the input character*/

isalnum function

The function detects letters or numbers. If the parameter is a letter or a number in the alphabet, the function returns a non-zero value, otherwise it returns zero. Include the header file ctype.h. The function is defined as follows:

int isalnum( int ch );

For example, the method of judging whether the input characters are numbers or letters:

char c;   /* Define character variables*/

scanf( "%c", &c );   /* Input character*/

isalnum(c);   /* Call isalnum function to judge the input character*/

Every word

Poetry and wine in time

Tags: function C++ encapsulation C

Posted by pelleas1022 on Wed, 30 Jun 2021 04:05:53 +0930