Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I bring the value of variables in one function to another?

0 votes
asked May 1, 2019 by anonymous
Hey, sorry if this is a simple question but I'm pretty new to programming. I'm making kind of a simple program that basically does the quadratic formula with given inputs a, b, and c (ax^2+bx+c). I made a bool function to test to see if the square root is a rational number or not. I have all of my inputs in the bool function and I don't know how to copy the value of those variables to my main function. Just an FYI, like i said earlier, my understanding of programing is pretty basic but I want to learn more, so over explain anything if possible! Can provide my (probably very bad) code if that will help!

3 Answers

0 votes
answered May 2, 2019 by arega assefa (140 points)
what is function?
0 votes
answered May 6, 2019 by Zander (140 points)
I would have to see the function to know what to do.
0 votes
answered May 11, 2019 by Saptarshi (140 points)
I am giving this answer with respect to Java. If you have done the program in C or python please mention it.

The main function cannot access other non-static functions until an object is created. Declare the variables outside the function if you want to. And to get the values of the variables you can access them via the object. Here's an example :

public class Main
    int var = 0; // Initially set to 0
    public static void main(String[] args) {
        System.out.println("Hello World");
        Main obj = new Main(); // Object created
        obj.changeVar(); // Method called, value of variable changed
        System.out.println(obj.var); // Value printed
    void changeVar() {
        var = 6;

See the comment lines.

Hope this helps.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.