Thread: Defining a function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    25

    Defining a function

    I'm trying to define a function in C++ and according to jumping into C++ i need to follow the format of

    Code:
    int convert(int var){
                int var = 0;
                var = var * 100 / 255;
                return var;
            };
    So thats the code I put in, and with that I don't get an error when I try to call the function, but at the same time the function doesn't do what it is supposed to do.

    Not really sure what i'm doing wrong here.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One problem is that you have a parameter named var, but then you also declare a local variable named var. You probably only want to keep the parameter. The next potential problem is that you are dealing with ints, so you will be using integer division, which might not be the right thing to do here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    25
    well if i don't declare var i get a compile error saying i used a undeclared variable. also could you explain a bit on the integer division?
    basically what i'm trying to do is write a program that converts DMX color values ( 0-255 ) to a 100% value using the formula "value * 100 / 255"
    I already have the program working, but i'm trying to build upon this simple program that i understand in order to learn most of the other programming concepts.
    which in this case happens to be defining a function.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zach Sisk
    well if i don't declare var i get a compile error saying i used a undeclared variable.
    That would be true if you did not have a parameter named var, but you do.

    Quote Originally Posted by Zach Sisk
    also could you explain a bit on the integer division?
    Basically, truncation, e.g., 5 / 2 = 2.5, mathematically, but 2.5 is truncated to 2, so in C++, 5 / 2 == 2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    25
    Any ideas as to why it wouldn't be working for me?
    Here is the code in context.
    Code:
    //
    //  main.cpp
    //  RGB Converter - C++
    //
    //  Created by Zach Sisk on 9/25/12.
    //  Copyright (c) 2012 Zach Sisk. All rights reserved.
    //
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    
    int main(int argc, const char * argv[])
    
    {
        string repeat = "n";
        
        do {
            
                   
                    int red = 000;
                    int green = 000;
                    int blue = 000;
                    
                    cout<<"Enter a Red DMX value\n";                                                                //Get a Red DMX value from user
                    cin>> red;
                        
                    
                    cout<<"\nEnter a Green DMX value\n";                                                            //Get a Green DMX value from user
                    cin>> green;
                        
                    
                    cout<<"\nEnter a Blue DMX value\n";                                                             //Get a Blue DMX value from user
                    cin>> blue;
            
                        
                                    do{
                                        if (red > 255 || red < 0){                                                              //Check to see if less that 255 or greater than 0
    
                                                cout<<"Enter a Red DMX value\n";        
                                                cin>> red;                                                                      //If not ask for new value
                                            }
                                            
                                        if (green > 255 || green < 0) {                                                         //Check to see if less that 255 or greater than 0
    
                                                cout<<"\nEnter a Green DMX value\n";                                            //If not ask for new value
                                                cin>> green;
                                            }
                                            
                                        if (blue > 255 || blue < 0){                                                            //Check to see if less that 255 or greater than 0
    
                                                cout<<"Enter a Blue DMX value\n";                                               //If not ask for new value
                                                cin>> blue;
                                            }
                                        
                                    } while (((red > 255 || red < 0)||(green > 255 || green < 0)||(blue > 255 || blue < 0)));   //Repeat while red, green, or blue is less than 0 or greater than 255
            
                           
                        int convert(int var){
                            var=var *100/255;               /*function in question*/
                            return var * 100 / 255;
                        };
                        
            
            convert(red);
            convert(green);
            convert(blue);
                      
                    
                    cout<<"Red: " << red << " " << "Green: " << green << " " << "Blue: " << blue <<"\n";       //Display converted Values
                        
                    cout<<"\nConvert Another?\n";      // Ask to convert another
                    cin>> repeat;
        
        
         } while (repeat == "y" || repeat == "yes" || repeat == "Y" || repeat == "Yes");        //Conditional Repeat
    
        return 0;
    }
    And that fails compiling due to a undeclared identifier 'var'

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be defining your convert function outside of your main unction, in this case, before your main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    25
    Quote Originally Posted by laserlight View Post
    You should be defining your convert function outside of your main unction, in this case, before your main function.
    So after a lot of messing with it, the function is declared outside of main now.

    Now my problem lies with actually calling the function.
    I called the function and then had it output the functions input variable, but i got the value that was assigned through cin, not through the convert function.

    Is there a way that I can tell it to reassign itself to the variable that it came from?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code, the test input, expected output and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    25
    Code:
    //
    //  main.cpp
    //  RGB Converter - C++
    //
    //  Created by Zach Sisk on 9/25/12.
    //  Copyright (c) 2012 Zach Sisk. All rights reserved.
    //
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int convert(int var, int red){
        red = var * 100 / 255;
        return red;
        
    };
    
    int main(int argc, const char * argv[])
    
    {
        string repeat = "n";
        
        do {
            
                   
                    int red = 000;
                    int green = 000;
                    int blue = 000;
                    
                    cout<<"Enter a Red DMX value\n";                                                                //Get a Red DMX value from user
                    cin>> red;
                        
                    
                    cout<<"\nEnter a Green DMX value\n";                                                            //Get a Green DMX value from user
                    cin>> green;
                        
                    
                    cout<<"\nEnter a Blue DMX value\n";                                                             //Get a Blue DMX value from user
                    cin>> blue;
            
                        
                                    do{
                                        if (red > 255 || red < 0){                                                              //Check to see if less that 255 or greater than 0
    
                                                cout<<"Enter a Red DMX value\n";        
                                                cin>> red;                                                                      //If not ask for new value
                                            }
                                            
                                        if (green > 255 || green < 0) {                                                         //Check to see if less that 255 or greater than 0
    
                                                cout<<"\nEnter a Green DMX value\n";                                            //If not ask for new value
                                                cin>> green;
                                            }
                                            
                                        if (blue > 255 || blue < 0){                                                            //Check to see if less that 255 or greater than 0
    
                                                cout<<"Enter a Blue DMX value\n";                                               //If not ask for new value
                                                cin>> blue;
                                            }
                                        
                                    } while (((red > 255 || red < 0)||(green > 255 || green < 0)||(blue > 255 || blue < 0)));   //Repeat while red, green, or blue is less than 0 or greater than 255
              
            
                    convert(red,red);
                    convert(green, green);
                    convert(blue, blue);
            
                    
                    cout<<"Red: " << red << " " << "Green: " << green << " " << "Blue: " << blue <<"\n";       //Display converted Values
                        
                    cout<<"\nConvert Another?\n";      // Ask to convert another
                    cin>> repeat;
        
        
         } while (repeat == "y" || repeat == "yes" || repeat == "Y" || repeat == "Yes");        //Conditional Repeat
    
        return 0;
    }
    That is the test code and i've been inputing 255, 128, 0 which should output values of 100 50 and 0 respectively. and instead of giving me those values it gives me 255, 128, and 0..

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Consider this function:
    Code:
    int convert(int var, int red){
        red = var * 100 / 255;
        return red;
         
    }
    No matter what the value of the red parameter, you immediately overwrite it by storing the result of an expression into it. You could have defined the function as:
    Code:
    int convert(int var) {
        return var * 100 / 255;
    }
    Now, look at how you call the function:
    Code:
    convert(red,red);
    You did not make use of the return value, so the value of red remains the same. Rather, with my modified function, you could write:
    Code:
    red = convert(red);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    25
    That last part is what i needed to see. I tried it with only one variable to begin with with the same results, and i was trying to figure out how to assign it back to the variable it came from. Thanks for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with defining function
    By curious in forum C Programming
    Replies: 17
    Last Post: 06-01-2009, 10:05 PM
  2. Help! Defining a Function
    By assaf2b in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2008, 09:07 AM
  3. Defining a custom function
    By Nalif in forum C Programming
    Replies: 3
    Last Post: 10-01-2006, 12:40 PM
  4. Defining main function!
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 02:00 PM
  5. Defining function arguments
    By Vicious in forum C++ Programming
    Replies: 6
    Last Post: 09-13-2004, 05:29 AM