Thread: declaring and intilizing w/in functions

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    13

    declaring and intilizing w/in functions

    When declaring and initilizing variables: if we put it in the function it would reinitilize it each time the function is called... but also if in main or before main when the function is 'implemented' doesn't seem to work either.

    I get "Debug Error!
    Run-Time Check Failure #3 - The variable 'j' is being used without being initilized."

    Code:
    // INCLUDES AND NAMESPACES
    #include <iostream>
    #include <iomanip>
    using namespace std;
     
    // PROTOTYPES
    int FibonacciIterative(int);
    int FibonacciRecursive(int);
     
    // MAIN
    int main() {
        int i = 0;
        int j = 0;
        int k = 1;
        int l = 1;
        
        for (int i = 0; i < 20; i++) {
            cout << i << '\t';
            FibonacciIterative(i);
            FibonacciRecursive(i);
            cout << '\nl';
        }
    
    
        cout << "test output";
    
    
        return 0;
    }
     
    // FUNCTION IMPLEMENTATIONS
    // returns F_i of the Fibonacci sequence (iterative)
    int FibonacciIterative(int i) {
    
    
        int j;
        int k;
        int l;
    
    
        if (i == 0) {
            j = 0;
            k = 1;
            l = 1;
            cout << j << '\t';
        }
    
    
        l = j + k;
        j = k;
        k = l;
        cout << l << '\t';
        return l;
            
    
    
    }
     
    // returns F_i of the Fibonacci sequence (recursive)
    int FibonacciRecursive(int i) {
        cout << "test output two";
        return i;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Do you understand the meaning of "variable scope"?

    If you don't, look it up. If you don't understand variable scope, you will never understand any explanation of why your code isn't working.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    13
    Quote Originally Posted by grumpy View Post
    Do you understand the meaning of "variable scope"?

    If you don't, look it up. If you don't understand variable scope, you will never understand any explanation of why your code isn't working.
    Okay, thanks. I thought I had already tried it before main (as global) just because I was trying it everywhere but now I understand why.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There's a bit more to it than that.

    In your example, can you explain what the difference is between in i main() and i in either of the functions? Can you do the same for j (and other variables)?

    Although there are occasions where it is useful to make variables global, more often than not it is a really bad idea. If you answer my questions about the differences between variables of the same name, you'll also be closer to understand how to address your problems without using globals.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    13
    Quote Originally Posted by grumpy View Post
    In your example, can you explain what the difference is between in i main() and i in either of the functions? Can you do the same for j (and other variables)?
    The i in main is basically a counter.? (though there must be more to it than that since the recursion is going to be tied to it - I had the layout neat before switching the third column to recursion. Having the second two columns of iteration worked fine but the assignment requires each method).
    The i in main is global and passed to the function.

    The other variables just hold the numbers/integers for the calculations. I am thinking if I use the returned variable to main from the functions, then cout them, I could use local variables... and reuse the same variables. I also think that is what the teacher is intending us to do, otherwise there is no need to return the data (aside to end the function). But then if I declare them in the function and initilize them they will be reinitilized each time the function is called, like starting from the beginning right?

    Code:
     /*
    Author : Calvin
    Description : displays first 21 numbers in the Fibonacci sequence
    */
    
    
    
    
    // INCLUDES AND NAMESPACES
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    // GLOBAL VARIABLES
    int i = 0;
    
    
    int j = 0;
    int k = 1;
    int l = 1;
    
    
    int m = 0;
    int n = 1;
    int o = 1;
     
    // PROTOTYPES
    int FibonacciIterative(int);
    int FibonacciRecursive(int);
     
    // MAIN
    int main() {
        int i = 0;
        cout << "LETS LOOK AT THE FIBONACCI SEQUENCE...\n\n";
        cout << "ELEMENT\t\tITERATIVE\tRECURSIVE\n";
        for (int i = 0; i <= 20; i++) {
            cout << i << "\t\t";
            FibonacciIterative(i);
            FibonacciRecursive(i);
        }
    
    
        return 0;
    }
     
    // FUNCTION IMPLEMENTATIONS
    // returns F_i of the Fibonacci sequence (iterative)
    int FibonacciIterative(int i) {
    
    
        if (i == 0) {
            j = 0;
            k = 1;
            l = 1;
            cout << l-1 << "\t\t";
        }
    
    
        else {
        cout << l << "\t\t";
        l = j + k;
        j = k;
        k = l;
        
        }
        return l;
        
    }
     
    // returns F_i of the Fibonacci sequence (recursive)
    int FibonacciRecursive(int i) {
    
    
        if (i == 0) {
            m = 0;
            n = 1;
            o = 1;
            cout << o-1 << '\n';
        }
    
    
        else if (i < 2) {
            cout << o << "\n";
            o = m + n;
            m = n;
            n = o;
        }
    
    
        else {
            o = FibonacciRecursive(i-1) + FibonacciRecursive(i-2);
            cout << o << "\n";
        }
    
    
        return o;
    }

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You missed my point completely. I was NOT asking what you are using the variables to represent.


    In the code in your opening post there are declarations of i,j,k, and l in main(). Within the function FibonacciIterative() there are also declarations of i (as a function parameter), as well as declarations of j,k, and l.

    What is the relationship, if any, between i in main() and i in FibonacciIterative()? Also, what is the relationship, if any, between j in main() and j in FibonacciIterative()?

    When I use the word "relationship", I am not referring to values that the variables hold.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Apr 2014
    Posts
    13
    Quote Originally Posted by grumpy View Post
    In the code in your opening post there are declarations of i,j,k, and l in main(). Within the function FibonacciIterative() there are also declarations of i (as a function parameter), as well as declarations of j,k, and l.

    What is the relationship, if any, between i in main() and i in FibonacciIterative()? Also, what is the relationship, if any, between j in main() and j in FibonacciIterative()?
    The i in main is passed to the function so there is a close/tied relationship. Therefore I declared it globally and can remove the main declaration. It is not declared in the functions.

    There is no relationship between the j or k (from Fib iterative) or the m or n (from Fib recursive) and main or each other.

    The other two variables from the functions (l and o) are returned to main but main is currently not dependant on them; unless I output the result in main rather than from the function.

    Am I getting closer? I googled a little on it but mostly got info on the relationship between variables (causal, correlation, affine...). It seems to be a pretty direct relationship with i and none for the rest as they are not used in main.

    (Since I need the variable retained for the further calls to the functions do I need them global so they don't get re-initilized? I can't declare them in the if statement but I did try so they would only be initilized the first time.)

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Calvin Stancill View Post
    The i in main is passed to the function so there is a close/tied relationship.
    That's an artifact of how main() calls the function, nothing else. There is nothing preventing main() passing the value of another variable. The functions will still refer to that value as i, even if main() passes the value of a variable named foobar.

    Quote Originally Posted by Calvin Stancill View Post
    Therefore I declared it globally and can remove the main declaration. It is not declared in the functions.
    The function declaration also declares the arguments of the function.

    Using globals as a mechanism to avoid passing values as arguments is generally a poor idea though.

    Quote Originally Posted by Calvin Stancill View Post
    There is no relationship between the j or k (from Fib iterative) or the m or n (from Fib recursive) and main or each other.

    The other two variables from the functions (l and o) are returned to main but main is currently not dependant on them; unless I output the result in main rather than from the function.
    Your comments here are correct, albeit in context of code you posted after your first post (which I didn't actually ask about).

    Quote Originally Posted by Calvin Stancill View Post
    Am I getting closer? I googled a little on it but mostly got info on the relationship between variables (causal, correlation, affine...). It seems to be a pretty direct relationship with i and none for the rest as they are not used in main.
    There is actually no specific relationship between the variable i in main() and i in your functions. main() passes the value of its i to the function, and the called function receives that variable in something named i (the argument) - which has no relationship (except that the value of one has been passed to the other).

    Quote Originally Posted by Calvin Stancill View Post
    (Since I need the variable retained for the further calls to the functions do I need them global so they don't get re-initilized?
    No you don't. An alternative is for the function to accept additional arguments. If those arguments are pointers, and the address of appropriate variables are passed by the caller, values can be passed back to the caller.

    The difference is that each (recursive) call of a function can be self-contained - in the sense of all inputs and outputs going via function arguments. Using globals introduces a back channel for values to pass to and from the function - and between recursive calls of it - but also limit flexibility in terms of what the function can do with the variable.

    Quote Originally Posted by Calvin Stancill View Post
    I can't declare them in the if statement but I did try so they would only be initilized the first time.)
    Yes, indeed. Declaration of variables within an if() statement is forbidden.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions without declaring
    By r_james14 in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 10:01 AM
  2. declaring and using functions in a class
    By quiet_forever in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2007, 02:22 PM
  3. Syntax for Declaring Functions in Dev-C++
    By Muz in forum C Programming
    Replies: 4
    Last Post: 07-03-2007, 04:53 AM
  4. Declaring multiple functions
    By samuel in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2001, 12:15 AM
  5. Declaring Functions
    By Thantos in forum C Programming
    Replies: 3
    Last Post: 09-25-2001, 10:24 PM