Is this used correctly?

This is a discussion on Is this used correctly? within the C++ Programming forums, part of the General Programming Boards category; Code: / Functions calculate the area and the perimeter of a lot given // the length and the width; #include<iostream> ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Is this used correctly?

    Code:
    / Functions calculate the area and the perimeter of a lot given
    // the length and the width;
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    // Prototypes Here
    double funcarea(double length, double width)
    {
        return length * width
    }
    
    double funcperim(double length, double width)
    {
        return * length * 4
    }
    
    
    
    int main()
    {
    	
    	double length;
    	double width;
    	double area;
    	double perimeter;
    	
    	cout << "Enter length of lot";
    	cin >> length;
    	cout << "Enter width of lot";
    	cin >> width;
    
    	// do calculations
    
    	
    	cout << "The area of the lot is: " << area << endl;
    	cout << "The perimeter of the lot is: " << perimeter << endl;
    	return 0;
    }


    Am I using the prototypes in this code correctly?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    No. You don't have prototypes in that code. You defined the functions above main() which is where they are used. If you wanted to use prototypes, you would define the functions below main and add the prototype above main.

    That's the point of the prototype. You tell the code below what the function looks like, then actually write the full function later.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,297
    Not precisely, Daved.

    A prototype (in the language of the standard this is a declaration of the function) is an interface specification: it gives the name of the function, the types of arguments, and return values. That is all the information a compiler needs in order to check that usage is valid with the interface. In C++, the compiler must have encountered a function declaration before it encounters code that attempts to call that function. The compiler makes sure usage maps the interface, and emits object code to do the call, but does not need the function implementation.

    The function implementation (aka the definition of the function) is not needed where the function is called. It is (in rough terms) needed in order for the linker to be able to put compiled objects together, and produce an executable file. The linker simply generates linkages between the code that calls a function, and the entry point of that function. The important thing, however, is that the compiler does not need to have access to the function definition in order to call it.

    What you described is a special case, and would look like this .....
    Code:
    include<iostream>
    #include<iomanip>
    using namespace std;
    
    // Prototypes Here
    double funcarea(double length, double width);
    double funcperim(double length, double width);
    
    int main()
    {
    	double length;
    	double width;
    	double area;
    	double perimeter;
    	
    	cout << "Enter length of lot";
    	cin >> length;
    	cout << "Enter width of lot";
    	cin >> width;
    
    	// do calculations
    
           area = funcarea(length, width);
           perimeter = funcperim(length, width);
      	
    	cout << "The area of the lot is: " << area << endl;
    	cout << "The perimeter of the lot is: " << perimeter << endl;
    	return 0;
    }
    
    double funcarea(double length, double width)
    {
        return length * width;
    }
    
    double funcperim(double length, double width)
    {
        return length * 4;
    }
    (Note that I removed some syntax errors from MuzicMedia's code in the process of producing the above).

    However, the compiler does not need the implementations of functions to do it's stuff. The more general way of using prototypes is to put functions into separate source files. A common way is to place the prototypes into a header file (let's call it func.h)
    Code:
    #ifndef FUNC_H_INCLUDED
    #define FUNC_H_INCLUDED
    // Prototypes Here
    double funcarea(double length, double width);
    double funcperim(double length, double width);
    #endif
    and then have the source file for main() as follows;
    Code:
    #include<iostream>
    #include<iomanip>
    #include "func.h"
    using namespace std;
    
    int main()
    {
    	double length;
    	double width;
    	double area;
    	double perimeter;
    	
    	cout << "Enter length of lot";
    	cin >> length;
    	cout << "Enter width of lot";
    	cin >> width;
    
    	// do calculations
    
           area = funcarea(length, width);
           perimeter = funcperim(length, width);
      	
    	cout << "The area of the lot is: " << area << endl;
    	cout << "The perimeter of the lot is: " << perimeter << endl;
    	return 0;
    }
    and a separate source file for implementing the functions.
    Code:
    #include "func.h"    // bring in the prototypes
    
    double funcarea(double length, double width)
    {
        return length * width;
    }
    
    double funcperim(double length, double width)
    {
        return length * 4;
    }
    The usage of the header files (which draws in the "prototypes") is not strictly needed in this second file, but is a VERY good idea practically in order to ensure that the function definitions correspond to their declarations (i.e. the function implementation match the prototypes).

    Given the two source files and header, the sequence to build an executable would be;
    1) Compile each source file, to produce corresponding object files.
    2) Link the two object files (and any other required libraries or object files) together to produce the executable.

    Note: before anyone accuses me of telling fibs, the approach has to be a bit different for template functions.
    Last edited by grumpy; 03-08-2008 at 06:42 PM.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Why do you need to reprint the prototype after the return?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MuzicMedia View Post
    Why do you need to reprint the prototype after the return?
    Read it again.

    A prototype:
    Code:
    double funcarea(double length, double width);
    A function:
    Code:
    double funcarea(double length, double width)
    {
        return length * width;
    }
    Notice that they are not actually the same thing.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Unhappy

    Code:
    #include<iostream>
    #include<iomanip>
    #include<stdlib.h>
    using namespace std;
    
    // prototype goes here
    
        double calcint(double amount, double rate);
    
        int year;
    	double rate;
    	double amount;
    
    int main()
    {
    	// declare variables
    	
    	year = 12;
    	
    	// get user input
    	cout << "Enter loan amount: " << amount << cin << amount << endl;
    	cout << "Enter loan rate: " << rate << cin << rate << endl;
    
    	// call function for calculation
    	calcint(amount, rate);
    	cout << "This is your annual intrest rate: " << calcint << endl;
    	
    	// display results
    	system("pause");
    	
    	return 0;
    }
    
    double calcint(double amount, double rate)
    {
    	return amount * rate * year;
    }
    I'm having the toughest time understanding why I cannot use prototypes rightfully.
    Someone please help, I've started to understand some, from other helpful programmers but I dont fully comprehend.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MuzicMedia View Post
    Code:
    #include<iostream>
    #include<iomanip>
    #include<stdlib.h>
    using namespace std;
    
    // prototype goes here
    
        double calcint(double amount, double rate);
    
        int year;
    	double rate;
    	double amount;
    
    int main()
    {
    	// declare variables
    	
    	year = 12;
    	
    	// get user input
    	cout << "Enter loan amount: " << amount << cin << amount << endl;
    	cout << "Enter loan rate: " << rate << cin << rate << endl;
    
    	// call function for calculation
    	calcint(amount, rate);
    	cout << "This is your annual intrest rate: " << calcint << endl;
    	
    	// display results
    	system("pause");
    	
    	return 0;
    }
    
    double calcint(double amount, double rate)
    {
    	return amount * rate * year;
    }
    I'm having the toughest time understanding why I cannot use prototypes rightfully.
    Someone please help, I've started to understand some, from other helpful programmers but I dont fully comprehend.
    You should move red to rejoin its comment in blue.

    Fortunately, you now have the prototypes correct.

    Unfortunately, you've lost year. Add that as a parameter to your function (in both the prototype and the definition).

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by MuzicMedia View Post
    Code:
    double funcperim(double length, double width)
    {
        return * length * 4
    }
    Note: That is an incorrect calculation for the perimeter of a rectangle. Not all 4 sides have the same length.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Did I Install the Platform SDK Correctly?
    By bengreenwood in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2008, 09:33 AM
  2. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  3. Do I understand pointers correctly?
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 01-05-2006, 07:30 AM
  4. windows header files not linking correctly
    By skorman00 in forum Windows Programming
    Replies: 2
    Last Post: 04-13-2005, 11:14 AM
  5. Why does this not read in correctly?
    By bluebob in forum C Programming
    Replies: 9
    Last Post: 04-19-2002, 09:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21