Correcting error in function call by reference – CSKC
Sign In

Search

Solved Problem! #CPP#2742    siteicon   siteicon  

Problem Statement - Correcting error in function call by reference

Program given below has some incorrectly written function call, correct the mistake and rewrite the
program.

TC++ #2742

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Solution


iseven(5) is incorrectly written as the function is being called as a reference and here a constant integer has been provided. so the call must be given as below –
int x=5; and then if(iseven(x)) …..

The corrected program will be –

#include <iostream>
using namespace std;
int iseven(int& a)
{
 if(a%2==0) return 1;
 else return 0;
}
int main()
{
 int x=5; 
 if(iseven(x)) cout<<"even";
 else cout<<"odd";
 return 0;
}

 

Notes

  • Please note that whenever we declare a variable say int x=0; we actually name the location name as x where the variable of integer type 0 is stored. So if a reference type is passed to a function we can safely pass the variable name (which is actually an address location) and not the constant/literal value directly.


Share

Created: 17-Jan-2018 22:26 | Updated: 17-Jan-2018 22:33|