Alright, i hope someone can give me a straight answer.

How do i export integers to another function?
Because when i compile and run this code, i get an error telling me:
"

12 D:\test.cpp `numberr' undeclared (first use this function)

The code i used was kinda like this :

int Plus()
{
    
int all = number + number2;

return all;


}





int main()
{
    
    
    int number;
    int number2;
    int do;
    
    
    
    cout << "type in a number" << endl;
    cin >> number;
    
        cout << "another number" << endl;
    cin >> number2;
    
    
    
    
    cout << "+, - , * or /?" << endl;
    cin >> do;
    
    
    if(do == "+")
    {
            
            Plus();
            }
    system("pause");
    
    
    
}

I just learned how to use functions so don't blame me for newbie errors.

So just answer my main question:
How do i use variables that's in main(); in Plus(); ?

BTW, the code is just a test.

Recommended Answers

All 5 Replies

You have to pass variables as an argument.
If you want to pass one argument of type char to a function that returns bool you declare function: bool Foo(char a_char) And you call that function like (let's say you already have char letter inside main): Foo(letter); BUT, if you want to catch (to see) what your function RETURNS, then write this: bool fnc_returned = Foo(letter); I hope you won't have a problem implementing this in your code. Have fun :)

Well you need to pass number1 and number2 to your Plus function.

number and number2 are variables which are local to your main function, so Plus does not have access to them.

Try something like this for example:

int Plus(int number, int number2)
{  
int all = number + number2;
return all;
}

int main()
{  
    int number;
    int number2;
    int do;
      
    
    cout << "type in a number" << endl;
    cin >> number;
    
        cout << "another number" << endl;
    cin >> number2;
    
    
    cout << "+, - , * or /?" << endl;
    cin >> do;
    
    
    if(do == "+")
    {
            
            cout << " the total is" << Plus(number, number2) << endl;
            }
    system("pause");
    
    
    
}

>How do i export integers to another function?
Ideally you would import values through parameters and arguments, and export values through the return:

#include <iostream>

int foo() 
{
  return 10;
}

void bar ( int import )
{
  std::cout<< import <<'\n';
}

int main()
{
  bar ( foo() );
}

You should have a read about functions and parameters.

#include <iostream>

int Plus(int x, int y){
    return x + y;
}

int main(){
    int number;
    int number2;
    char op;
    
    std::cout << "type in a number" << std::endl;
    std::cin >> number;
    
    std::cout << "another number" << std::endl;
    std::cin >> number2;
    
    std::cout << "+, - , * or /?" << std::endl;
    std::cin >> op;
    
    if(op == '+'){
            std::cout << Plus(number, number2) << std::endl;
            }
            
    system("pause");
    return 0; 
}

I should point out you cannot read a '+' sign into an int, hence why i changed it to a char. Also you cannot use 'do' as a variable name as it is a command used in do..while loops.

Chris

edit:: damn 4 posts at once!

Thank you guys :)

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.