hello guys and hope you are fine :)

please take a look to this code

#include <iostream>
using namespace std;
int sum(int num1,int num2, int sum1){
	cout <<"Please enter two numbers:";
	cin >> num1 >> num2;
	sum1 = num1 + num2;
	cout << sum1;
	return 0;
	}
int div(int num3,int num4, int sum2){
	cout <<"Please enter two numbers:";
	cin >> num3 >> num4;
	sum2 = num3 / num4;
	cout << sum2;
	return 0;
	}
int main()
{
char q ='y'; // to quite the programme
int choose,num1,num2,num3,num4,sum1,sum2;
while(q == 'y'){
  cout <<"Welcome to the super programme"<<endl;
  cout <<"1- finding the sum of the numbers."<<endl;
  cout <<"2- finding the divide of the numbers."<<endl;
  cout <<"please choose which programe you want:"<<endl;
  cin >> choose;
  while(choose < 1 || choose > 2){
  cout<<"please choose ethir '1' or '2'";
  cin >> choose;
  }
  if(choose == 1){
  sum(num1,num2,sum1);
  }else if(choose == 2){
  div(num3,num4,sum2);
  }
  cout <<" ,Repeat?";
  cin >> q;
  }
    return 0;
}

this code is very simple, it's just ask a user to choose which programme he want whther to find the sum of two numbers or to find the Division

so my question now can i use the same name of each varible in each function?

this is an example,the num1,num2,sum1 are vaibles in the function'sum' but i want to be the same name in the div function?

second qustion, why i must declare the varibles of my functions in the main function again?

and is there any way to avoid declaring the varibles in the main function?

last question , can anybody show me the diffrernt in calling by value or refrence in my code?

waiting for you

why do those two functions have arguments? Did your instructor tell you to do that?

Move lines 4 and 5 down into main() before calling either of those two functions, delete lines 11 and 12, then you can use the same variables for both functions on lines 32 and 34. It's ok for main() to name the variables num1 and num2, and for the functions to name them something else.

Those two functions should return the results of the division or multiplication, and not return just 0.

but i want to be the same name in the div function?

What's stopping you?

why i must declare the variables of my functions in the main function again?

No you don't have to. Local variables are block specific(read: Variable Scope). The variables you declare in main() aren't accessible in any other function.
The variables your declare in functions are formal arguments & are copies of the original ones.
The diff. ways to pass a variable are.

Edit: The formal arguments are the arguments whom the function sets as an alias for the actual arguments.
The actual arguments are the arguments with which the function is called.

so my question now can i use the same name of each varible in each function?

Yes you can use this but you have to declare that variables globally.

Secondly you can send the same parameters in two functions but you have to intialize again before sending second time for eg:

cout<<"enter two numbers for addtion:";
cin>>num1>>num2;
sum(num1,num2);
cout<<"enter two numbers for division:";
cin>>num1>>num2;    //second time intialization for sending parameters
div(num1,num2);

now talking about your above programe why are sending parameters to function definition in line 32 and 34 while you are not sending any value, i think you should call function like this

if(choose == 1)
{  
sum();  
}
else if(choose == 2)
{
 div();  
}

and do not define any variable here like num1,num2........
it is better to define in function definition only

in case of multiple situations like you use

want whther to find the sum of two numbers or to find the Division

or some more situations it is better to use switch() statement it decreases the size of programe and can be read easily.

for switch()
http://www.intap.net/~drw/cpp/cpp04_02.htm

last question , can anybody show me the diffrernt in calling by value or refrence in my code?

If I remember correctly (for C++), you need to add '&' in front of variable type in the function prototype in order to change from calling by value to calling by reference. (i.e. int sum(&int num1, &int num2)) I believe you know the difference between call by value & reference.

thank you evry one

but i'm till having problem, can you see my current code

#include <iostream>
using namespace std;
int sum(int num1,int num2){
    int sum;
	sum = num1 + num2;
	return sum;
	}
int div(int num1,int num2){
    int sum;
	sum = num1 / num2;
	return sum;
	}
int main()
{
char q ='y'; // to quite the programme
int choose;
while(q == 'y'){
  cout <<"Welcome to the super programme"<<endl;
  cout <<"1- finding the sum of the numbers."<<endl;
  cout <<"2- finding the divide of the numbers."<<endl;
  cout <<"please choose which programe you want:"<<endl;
  cin >> choose;
  while(choose < 1 || choose > 2){
  cout<<"please choose ethir '1' or '2'";
  cin >> choose;
  }
  if(choose == 1){
  cout <<"Please enter two numbers:";
  cin >> num1 >> num2;
  sum(num1,num2);
  }else if(choose == 2){
  cout <<"Please enter two numbers:";
  cin >> num1 >> num2;
  div(num1,num2);
  }
  cout <<" ,Repeat?";
  cin >> q;
  }
    return 0;
}

whats the problem?

waiting for you :(

up

thanks i find the soultion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.