In: Computer Science
I will provide you 2 programs. 1st program in which the function has two parameters or arguments whereas in the 2nd program function without arguments. But both the program will provide you the same result
1st Program
#include<iostream> // defining header file
using namespace std;
int divideBy(int x,int y); //declaring the function with two arguments. Since the function returns integer it is declared in int type
int main() //main function
{
int a,b,c; //declaring variables
cout<<"Enter the dividend";
cin>>a;
cout<<"Enter the divisor";
cin>>b;
c=divideBy(a,b); //calling the functions by passing the values
cout<<c;
return 0;
}
int divideBy(int x,int y)
{ //body of the function starts here
int z;
if(y==0) //checking whether the divisor is 0
z=-1;
else
z=x%y; // % is modulus operator which is used to return the remainder value
return z; //returning value to the calling function
}
2nd Program
#include<iostream>
using namespace std;
int divideBy(); //declaring function without arguments
int main()
{
int a;
a=divideBy();
cout<<a;
return 0;
}
int divideBy()
{
int x,y,z;
cout<<"Enter the dividend" //getting the inputs inside the function
cin>>x;
cout<<"Enter the divisor";
cin>>y;
if(y==0)
z=-1;
else
z=x%y;
return z;
}