Example about using functions:
// A value returned by a return statement in a function
#include <iostream>
using namespace std;
int funcRet1(void);
#include <iostream>
using namespace std;
int funcRet1(void);
int funcRet2();
int funcRet3();
int funcRet3();
int funcRet4(int z);
int main()
{
{
int num = 4;
cout<<"Line 1: Value returned by funcRet1: "<<funcRet1()<<endl; // Line 1
cout<<"Line 2: Value returned by funcRet2: "<<funcRet2()<<endl; // Line 2
cout<<"Line 3: Value returned by funcRet3: "<<funcRet3()<<endl; // Line 3
cout<<"Line 4: Value returned by funcRet4: "<<funcRet4(num)<<endl; // Line 4
return 0;
}
int funcRet1()
{
cout<<"Line 1: Value returned by funcRet1: "<<funcRet1()<<endl; // Line 1
cout<<"Line 2: Value returned by funcRet2: "<<funcRet2()<<endl; // Line 2
cout<<"Line 3: Value returned by funcRet3: "<<funcRet3()<<endl; // Line 3
cout<<"Line 4: Value returned by funcRet4: "<<funcRet4(num)<<endl; // Line 4
return 0;
}
int funcRet1()
{
return 23, 45; //Only 45 is returned
}
int funcRet2()
{
int funcRet2()
{
int x = 5; int y = 6;
return x, y; //Only the value of y is returned
return x, y; //Only the value of y is returned
}
int funcRet3()
{
int funcRet3()
{
int x = 5; int y = 6;
return 37, y, 2 * x; //Only the value of 2 * x is returned
return 37, y, 2 * x; //Only the value of 2 * x is returned
}
int funcRet4(int z)
{
int funcRet4(int z)
{
int a = 2; int b = 3;
return 2 * a + b, z + b; //Only the value of z + b is returned
return 2 * a + b, z + b; //Only the value of z + b is returned
}
0 comments:
Post a Comment