unicorny
unicorny

Reputation: 1

How to use a variable from other functions

int calculateX()
{
    
    int x;
    cout<<"Enter value for x: ";
    cin>>x;
    return x;
}

int anotherFunction()
{
    /////
}

int main()
{
    calculateX();
    anotherFunction();
    
    return 0;
    
}
    

This is an example on a code. How do can I possibly use the input from the user in the calculateX() function into the function anotherFunction() ?

Upvotes: 0

Views: 777

Answers (4)

Dipto Arafat
Dipto Arafat

Reputation: 1

Function generally has return type before it's name. For Example :

int calculateX() {   
    int x;
    cout<<"Enter value for x: ";
    cin>>x;   
    anotherFunction(x)
    return x;
}

int is here return type.And you are calling two functions from your main function. calculateX has a variable of type int named x. So, you can't access x from outside of calculateX scope. But you can return it to your main function and store it in a different variable so that It becomes available in main function. Once It becomes available, you can send it as argument to another function . In your case It should be like this :

int anotherFunction(int valueFromMain)
{
   std::cout << valueFromMain << std::endl;
}

int main(){
    int returnedFromCalculateX = calculateX(); //You have returned value available on main Function
    anotherFunction(returnedFromCalculateX); //Send It to anotherFunction(pass by value)
    return 0;    
}

You can also send the value as a reference. That needs a bit higher understanding. Please Check : https://www.w3schools.com/cpp/cpp_function_reference.asp for more details.

Upvotes: 0

Khải Hồ Quang
Khải Hồ Quang

Reputation: 70

You must declare the variable x global (outside the calculateX() function), so that anotherFunction() can see it. If you define the variable x inside the calculateX() function, it will be deleted after exiting the function.

int x;
int calculateX()
{   
    cout<<"Enter value for x: ";
    cin >> x;
    return x;
}

int anotherFunction()
{
    /////
}

int main()
{
    calculateX();
    anotherFunction();
    return 0;
    
}

Upvotes: 0

Nikhil Singh
Nikhil Singh

Reputation: 367

what you need is either a pass by value , which is already answered or a global variable like this

#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int anotherFunction()
    {
        cout << g;
    }
int main () {
   // Local variable declaration:
   int a, b;
   a = 10;
   b = 20;
   g = a + b;
   anotherFunction();
   return 0;
}

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.

Upvotes: 0

Viktor T&#246;r&#246;k
Viktor T&#246;r&#246;k

Reputation: 1319

You have to use function parameters:

int calculateX()
{    
    int x;
    cout<<"Enter value for x: ";
    cin>>x;
    return x;
}

int anotherFunction(int x)
{
    /////
}

int main()
{
    int x = calculateX();
    anotherFunction(x);
    
    return 0;    
}

Upvotes: 5

Related Questions