Thread: Prototypes?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Prototypes?

    My instructor wishes for me to construct prototypes for a small program but I'm having a time figuring this out. help please.

    // interest.cpp
    // Date: Feb 25, 2008
    // Calculate the simple interest on a loan for 1 year given the amount and rate
    #include<iostream>
    #include<iomanip>
    using namespace std;


    Code:
    //Prototypes here
    
    
    
    int main()
    {
    	// declare variables
    
    	// get user input
    
    	// call function for calculation
    
    	// display results
    
    	return 0;
    }
    
    double calcint(double amount, double rate)
    {
    	return amount * rate * 1;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Outside of writing the prototypes for you, what would you like us to do?

    Are you having trouble understanding what a function prototype is?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Are you having trouble understanding what a function prototype is?
    Yes i am. and could u finish the program so i can get a step by step basis of how to complete my programs. its a hybrid course so i have little help for the instructor. Thank you

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by MuzicMedia View Post
    Yes i am. and could u finish the program so i can get a step by step basis of how to complete my programs.
    That is your responsibility.
    http://en.wikipedia.org/wiki/Function_prototype
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by MuzicMedia View Post
    Yes i am. and could u finish the program so i can get a step by step basis of how to complete my programs. its a hybrid course so i have little help for the instructor. Thank you
    Yes, I could do your homework for you, but that doesn't help you learn anything. There are examples of code all over the web, which you can learn from. Assignments are for you to actually gain practice from doing the work.

    A function prototype is easy to understand if you know what a function is. Basically, a function prototype is used to tell the compiler that you have a function that you wrote. This lets the compiler store information on what types of variables it accepts and what type of variable it returns. When it comes across usage of your function and the definition of the function itself, it can verify that it matches the prototype. If it doesn't match, however, then the compiler can let you know something is wrong, and allow you the opportunity to go back and fix it.

  6. #6
    coder
    Join Date
    Feb 2008
    Posts
    127
    a hint for you:

    the following will work fine:
    Code:
    double calcint(double amount, double rate)
    {
    	return amount * rate * 1;
    }
    
    int main () {
    	//...
    	calcint (amount, rate);
    	//...
    	return 0;
    }
    the next code won't compile because the compiler doesn't know what calcint is:
    its declaration appears after the program calls it in main.
    Code:
    int main () {
    	//...
    	calcint (amount, rate);
    	//...
    	return 0;
    }
    
    double calcint(double amount, double rate)
    {
    	return amount * rate * 1;
    }
    a function prototype tells the compiler that calcint exists: it lets you implement your calcint anywhere:
    <solution removed>
    Last edited by CornedBee; 03-08-2008 at 03:18 AM.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Thank you. Does it always have to be double, because those are the only examples i seem to find?

    -After the calcint(amount, rate), do i have to put in parentheses which things are associated with the function prototype?

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Replied

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Writing prototypes is simple. Just select the first line of the function, copy it, paste it at the top and add a semicolon. Works in most cases.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by CornedBee View Post
    Writing prototypes is simple. Just select the first line of the function, copy it, paste it at the top and add a semicolon. Works in most cases.
    And will work in your case.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The prototype should just be a replica of how your function definition looks like, but with a ; at the end, as have already been implied. It's not tougher than that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    not a good practice remove var names from function prototypes - makes code harder to read
    Last edited by CornedBee; 03-08-2008 at 03:18 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    When they are member functions, I tend to leave the names in. When they are not member functions (usually when I'm actually writing in C), I tend to leave the names out.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MacGyver View Post
    When they are member functions, I tend to leave the names in. When they are not member functions (usually when I'm actually writing in C), I tend to leave the names out.
    And how do you know looking at
    char *strstr(const char *, const char *);
    Where should be put the full string and where the substring to be searched?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When they are not member functions (usually when I'm actually writing in C), I tend to leave the names out.
    What's your rationale for this practice?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function prototypes - needed yes or no?
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 08:39 AM
  2. Class prototypes
    By m00se123 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 03:06 AM
  3. Is there a difference between these prototypes?
    By Captain Penguin in forum C++ Programming
    Replies: 5
    Last Post: 06-11-2002, 10:28 AM
  4. function prototypes and call statements.
    By mutu in forum C Programming
    Replies: 0
    Last Post: 04-05-2002, 12:39 AM
  5. Default values in function prototypes
    By wdicks in forum C Programming
    Replies: 13
    Last Post: 10-10-2001, 01:06 AM