Prototypes?

This is a discussion on Prototypes? within the C++ Programming forums, part of the General Programming Boards category; My instructor wishes for me to construct prototypes for a small program but I'm having a time figuring this out. ...

  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,211
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  4. #4
    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

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

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  9. #9
    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)

  10. #10
    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 02:18 AM.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    not a good practice remove var names from function prototypes - makes code harder to read
    Last edited by CornedBee; 03-08-2008 at 02:18 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by laserlight View Post
    What's your rationale for this practice?
    In all honesty, probably nothing worth copying. I just posted what I have done in the past, and I'm open to changing it. Usually, it was done without much conscious thought.

    I try to write my classes inside headers, and I need to have an idea how all of my classes generally interact. I imagine I write the names out because of this, because it helps me figure out what the class is doing in each function when I'm writing the headers before I write any actual implementation code.

    In terms of C, I guess it's because I generally know from the name of the function and the types they take what is going on. Member functions have the added context of a class that they operate within. Non-member functions do not have that context. They operate in terms of the program (or the collection of other functions you're grouping them with), and I think potentially (depending on how you write your code of course) easier to figure out what they do.

    All in all, I've heard both ends.... it's good to leave the names in, and it's good to not leave the names in. In terms of people that need to study the headers or prototypes and want to learn how to use your code, it's probably best to leave the names in as hints as to how the function works.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 07:39 AM
  2. Class prototypes
    By m00se123 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 02: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-04-2002, 11:39 PM
  5. Default values in function prototypes
    By wdicks in forum C Programming
    Replies: 13
    Last Post: 10-10-2001, 01:06 AM

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