Self-cultivation for C++ lovers (10): Custom functions with return values

Today, I will share with you the custom functions with return values ​​in C++

1. A custom function with no input value and return value

The custom function of ps is actually useless, just to introduce

Format:

return value type function name(void){

statement;

return value;

}

For example:

int yanshi(void){
    int m = 54 * 65;
    return m;
}

Using this function can actually only output the value of 54×65, which is basically useless

2. Custom functions with input values ​​and return values

Format:

Return value type function name (data type variable name) {

statement;

return value;

}

Example: Make a small program that converts Celsius to Fahrenheit

#include<iostream>
using namespace std;
//下面就是自定义函数部分
double centigrade(double C )
{
    double F = C*1.8+32;
    return F;//返回F的值
}
//上面就是自定义函数部分
int main()
{
    double F;
    cout<<"请输入摄氏温度"<<endl;
    cin>>F;
    cout<<"华氏温度是"<<centigrade(F)<<endl;
    return 0;
}

The above is the knowledge sharing~

Remember to like it!

Guess you like

Origin blog.csdn.net/pyz258/article/details/129468576