C++ classic function example-function definition and calling method, overloaded function and use of system function

1. Function definition format:
return type function name (parameter list)
{ function body; }

  • The return type of a custom function can be int, double, char, etc. If there is no return value, the type can be void.
  • The function name is an identifier, and its name should be named according to the rules of identifiers.
  • There can be one or more parameters in the parameter list. The parameters are separated by commas. Each parameter is composed of parameter type and parameter name. The parameter list can also be empty.
  • Except for custom functions whose return type is void, at least one statement in the function body is a return expression to return the value of the function.

Example questions:
1. Write a function to convert Fahrenheit temperature to Celsius temperature, the conversion formula is: C=(F-32)*5/9

#include<iostream>
using namespace std;
//定义一个转换温度的函数
int exchange(int F)
{
    
       
 int C;    
 C=(F-32)*5/9;	 
 return C;
 }
 void main()
 {
    
    	
 int x;	
 cin>>x;
 cout<<exchange(x)<<endl;//在主函数中调用exchange()函数
 }

Input: 32
Output: 0

2. Overloaded function
Overloaded function is a special case of function. For ease of use, C++ allows several functions with the same name to be declared in the same scope, but the formal parameters of these functions with the same name (refer to the number and type of parameters) Or order) must be different, that is to say, use the same function to complete different functions. This is the overloaded function.

Example:
2. Write the overload function max1 to obtain the maximum value of two integers, three integers, two floating-point numbers, and three floating-point numbers respectively.

#include<iostream>
using namespace std;
int maxl(int x ,int y)//返回两个整数中的最大值
{
    
    
return (x>y?x:y);
}
int maxl(int x,int y,int z)//返回三个整数中的最大值
{
    
      
 int t;
 t=maxl(x,y);
 return (t>z?t:z);
 }
 float maxl(float x,float y)//返回两个浮点数中的最大值
 {
    
    	
 return (x>y?x:y);
 }
 float maxl(float x,float y,float z)//返回三个浮点数中的最大值
 {
    
       
        int t;
 	t=maxl(x,y);	
 	return (t>z?t:z);
 	}
 void main()
 {
    
    
 int a,b,c;
 float x,y,z;
 cout<<"输入两个整数:"<<endl;
 cin>>a>>b;
 cout<<"大:"<<maxl(a,b)<<endl;
 cout<<"输入三个整数:"<<endl;
 cin>>a>>b>>c;
 cout<<"大:"<<maxl(a,b,c)<<endl;
 cout<<"输入两个浮点数:"<<endl;
 cin>>x>>y;cout<<"大:"<<maxl(x,y)<<endl;
 cout<<"输入三个浮点数:"<<endl;
 cin>>x>>y>>z;
 cout<<"大:"<<maxl(x,y,z)<<endl;
 }

Eg:
Enter two integers: 2 9
large: 9
enter three integers: 3 8 6
large: 8
enter two floating-point numbers: 3.0 9.7
large: 9.7
enter three floating-point numbers: 4.6 8.9 7.3
large: 8

The four functions declared in the above program are overloaded functions for each other. They meet one or two of the two conditions of different parameter types and different number of parameters. The compiler will automatically find the one to be called according to the different parameters. function.

3. Use the system function pow(x,y) to calculate the value of xy, pay attention to including the header file math.h.

#include<iostream>
#include<math.h>
using namespace std;
void main()
{
    
    	
int x,y;	
cout<<"x=";	
cin>>x;	
cout<<"y=";	
cin>>y;
cout<<"x^y="<<pow(x,y)<<endl;
}

Eg:
x=3
y=2
x^y=9

4. Calculate the factorial of 3

#include<iostream>
using namespace std;
int fun(int x)
{
    
    
int N,s=1;
for(N=1;N<=x;N++)
{
    
    s=s*N;}
return s;
}

void main()
{
    
    int x,N;
cin>>N;
x=fun(N);
cout<<N<<"!="<<x<<endl; 
	}

Input: 3
Output: 3! =6

Experience:
1. In the same scope, you can declare several functions of the same name with similar functions, but the formal parameters (referring to the number, type or order of the parameters) of these functions with the same name must be different. Functions cannot be overloaded only by the difference in return type.
2. The function call operator () can be overloaded for objects of the class. When overloading (), you are not creating a new way to call a function, on the contrary, this is creating an operator function that can pass any number of parameters

Guess you like

Origin blog.csdn.net/haha_7/article/details/109012656