Thread: I Can't Take This Anymore

  1. #1
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    Angry I Can't Take This Anymore

    I've spent all day trying to learn exactly how seperate functions work. For some reason, I'm having a lot of trouble in this area. I need some answers.

    Are the function parameters even useful?

    If so, can they be used as local variables within the function?

    If I wanted a function to return Area, which is and intenger that is assigned the value of length * height, where would I declare these variables? Within the local function? Should they be global variables? Should I declare their values in the main() function?

    It seems that whenever I have more then one function in my source code, I tend to get tons of errors because the certain parts of the code aren't in the right place. It seems I dont know some of the DOs and DONTs of functions and code involving seperate functions. Can some one give me some tips?

    Thanks

    P.S. -

    Since this is giving me so much trouble, I think I need a step-by-step guide to functions, and what proper function synthax is. Gimme some codes, and just sorta treat me like I'm a complete idiot. Anything. Just so I can learn this stuff, and get on with learning other things in C++
    -Dean

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Are the function parameters even useful?

    Absolutely. For example, to get the square of an integer, you could do this

    Code:
    int sqr(int x)
    {
      return x * x;
    }
    If you couldn't pass x into the function, what is it going to return?

    >If so, can they be used as local variables within the function?

    That's exactly what they are. In the example above, x is local in the sqr function.

    >If I wanted a function to return Area, which is and intenger that is assigned the value of length * height, where would I declare these variables? Within the local function? Should they be global variables? Should I declare their values in the main() function?

    Well you would define a function like this:

    Code:
    int area(int length, int height)
    {
      return length * height;
    }
    Length and height are declared as parameters in the area function.

    So to call this, you would do something like:

    int a = area(5, 5);

    In this case, with are using literals (5 & 5). But these could be replaced with variables local to (or in the scope of) the calling function.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Cool

    Here's a sample of how to use several functions and pretty much how to write a simple program.

    Code:
    #include <iostream>
    using namespace std;
    
    // constant definitions
    const double PI = 3.14159;
    
    // function prototypes
    double RectArea(double length, double width);
    double RectPerim(double length, double width);
    
    int main()
    {
    	// variable declarations
    	double length, width, area, perimeter;
    
    	// get data from the user
    	cout << "Enter the length: ";
    	cin >> length;
    	cout << "Enter the width: ";
    	cin >> width;
    
    	// Calculations
    	area = RectArea(length, width);
    	perimeter = RectPerim(length, width);
    
    	// Display results
    	cout << "The area is " << area << endl
    	 	<< "The perimeter is " << perimeter
    		<< endl;
    }
    
    // function implementations
    double RectArea(double length, double width)
    // description: calculates the area of the
    // rectangle
    // precondition: length and width are valid doubles
    // greater than zero
    // postcondition: returns the area of the rectangle
    {
    	return length * width;
    }
    
    double RectPerim(double length, double width)
    // description: calculates the perimeter of the
    // rectangle
    // precondition: length and width are valid doubles
    // greater than zero
    // postcondition: returns the perimeter of the
    // rectangle
    {
    	return 2 * length + 2 * width;
    }
    And there you have it. A sample program using two functions. Sorry about all of the comments. I did that for your learning benefit.

    -"Should they be global variables?"
    No, global variables are evil. They are diametrically opposed to the concept of data encapsulation. The only global identifiers you should use are constants.

    If you have any other questions or a more specific question, just post some code or your question and I'll try to be as helpful as I can. :-)

  4. #4
    Registered User nevermind's Avatar
    Join Date
    Oct 2002
    Posts
    23
    In oder to avoid errors always include function prototypes before teh main function like so
    Code:
    int Area (int , int);     
    OR
    int Area (int a, int b);
    The declaration or prototype simply tells the compiler what type to accept as values and what it expects to return so it can check for the correct types.

    The next part is off course the definition
    Code:
    int Area (int a, int b) {
       return a * b;
    }
    You then assign a variable the return value in function main(), when you call it.
    Code:
    int main() {
    
       int area;
       ...
       ...
    
       area = Area(2,3);
    
       ...
    }
    It is intersting to note that you can actually call functions without assigning the return values to a variable, just for the side effects.
    "Cut the foreplay and just ask, man."

  5. #5
    lurker1
    Guest
    Just think of functions as mini-programs. Here's an example of one I had to use for an assignment:

    Code:
    //This is the function prototype:
    
    void compute_coin(int coin_value, int& num_coins, int& amt);
    
    //This function will be used to calculate the amount of coins for
    //each coin (quarter, dime, nickel and penny). It will also reduce
    //the amount left to be calculated.
    
    
    //This is the function 'call' in the 'main' program:
    
      compute_coin(value_quarter, num_quarters, amt_left);
      compute_coin(value_dime, num_dimes, amt_left);
      compute_coin(value_nickel, num_nickels, amt_left);
      compute_coin(value_penny, num_pennies, amt_left);
    
    //This is how the function works (located after the end of the 'main' program:
    
    void compute_coin(int coin_value, int& num_coins, int& amt)
    {
    int total_coins_value;
    
    num_coins = amt / coin_value;
    total_coins_value = num_coins * coin_value;
    amt = amt - total_coins_value;
    
    return;
    Instead of writing out the calculations four times, I just had to write it once, then use it four times. They can be really helpful, if you have logic errors, it can be easier to narrow down where the problem is located.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ldconfig not working anymore?
    By patiobarbecue in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2009, 08:11 PM
  2. GCC does not compile anymore
    By arno-nyme in forum Linux Programming
    Replies: 3
    Last Post: 01-28-2009, 03:29 PM
  3. i dont even know what hacking means anymore
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 01-02-2007, 04:29 PM
  4. Cant edit my game anymore :(
    By FingerPrint in forum Game Programming
    Replies: 8
    Last Post: 10-14-2006, 08:06 AM
  5. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM