
如下面程序所示,在这个程序中,函数 divIDe 显示了 arg1 除以 arg2 的商。但是,如果 arg2 被设置为零,则函数返回到 main 而不执行除法计算。
#include <iostream>using namespace std;//Function prototypevoID divIDe(double arg1,double arg2);int main(){ double num1,num2; cout << "Enter two numbers and I will divIDe the first\n"; cout << "number by the second number: "; cin >> num1 >> num2; divIDe(num1,num2); return 0;}voID divIDe(double arg1,double arg2){ if (arg2 == 0.0) { cout << "Sorry,I cannot divIDe by zero. \n" ; return; } cout << "The quotIEnt is " << (arg1 / arg2) << endl;}程序输出结果:Enter two numbers and I will divIDe the first
number by the second number: 12 0
Sorry,I cannot divIDe by zero.
总结
以上是内存溢出为你收集整理的C++ return:使函数立即结束全部内容,希望文章能够帮你解决C++ return:使函数立即结束所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)