0

By this I mean that even though I define the function before the function call,is it required to have a function prototype? I tried a program with definition b4 call,it worked.but in C++ by Herbert schildt it says C++ requires full function prototyping.so am confused. So,does C++ really require full function prototyping?

agr
  • 127
  • 1
  • 12
  • 4
    If you look at the [book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), you'll see that none of H.Schildt's books appear. This has a reason: They're pretty bad. If you look at the [reviews](https://accu.org/index.php?module=bookreviews&func=search), you'll see that they are pretty much not at all recommended. Please use another book. – Rakete1111 Jul 09 '17 at 16:59
  • 2
    Yup, was just about to post the same thing - avoid Schildt texts at all costs. – Oliver Charlesworth Jul 09 '17 at 17:01
  • 3
    Use actual English instead of broken abbreviations in __all__ posts on stackoverflow. – Passer By Jul 09 '17 at 17:06

4 Answers4

6

A function definition is also a function declaration (i.e. a prototype). So if you define the function before you call it everything will be hunkydory. And I would strongly recommend not attempting to learn C++ from the works of Herb Schildt.

2

A function prototype or function interface is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body

a function prototype merely specifies its(the function) interface(I/O).

Prototype of a function is also called signature of the function.

If a call or reference has to be made to a function in a code section before the actual function definition, then the function prototype is compulsory.

A function prototype can be "discerned" or gotten from its definition, hence if a call is not made to the function before its actual definition, declaring the function prototype is not compulsory.

ytobi
  • 535
  • 1
  • 9
  • 19
0

By this I mean that even though I define the function before the function call,is it required to have a function prototype?

No, it doesn't mean that.

So,does C++ really require full function prototyping?

Yes it does. The compiler needs to see at least the declaration of a function before it can be used:

void foo(int i);

Alternatively you can provide a full (inlined) definition

inline void foo(int i) {
   // do something
}
user0042
  • 7,917
  • 3
  • 24
  • 39
  • 1
    A definition is also a declaration, so your wording is wrong and/or misleading, same for the example. – Rakete1111 Jul 09 '17 at 17:01
  • 2
    You don't need to inline the function if it is defined in the same .cpp file it is called from. The inline would only be needed when the function is defined in a header file. –  Jul 09 '17 at 17:02
  • @Neil _"if it is defined in the same .cpp file it is called from."_ You probably misunderstood my answer. I said _alternatively_ (by means of using the declaration). In such case you actually _should_ use `inline`, supposed it is defined in some header file. – user0042 Jul 09 '17 at 17:06
  • Thank you everyone – agr Jul 09 '17 at 17:58
0

Considering the question, I'll assume that you are at the beginner level. First of all, You should start learning from a better book.

Look at the compiler as an intelligent person who reads your program and converts it into machine language. When the compiler starts compiling your program it may encounter a function call such as: foo(); Now, it has to make the control jump to the function's address for the desired instructions to be executed during run but the compiler doesn't know what or where the function is! Providing the compiler with a reference helps it to compile your code.

Prototypes are such references, you could also define a function before any function calls and it would still work.

Other than technical reasons, prototypes makes code much more clean in case of big projects.

Otherwise, you would just be scrolling through Functions definitions having no idea what the functions are doing. Remember, you are like a human compiler too!

A usual mistake I've noticed: It is usual that people revolt this idea and define their functions on top. Although, when calling another function from a function, problems can arise.

int function_1()
{
        return function_2();
}

int function_2()
{
        return function_1();
}

Here, there are no arrangements of functions definitions which will work. Prototyping is much better.

Assumptions: There are obviously some more statements to break the endless loop and return values, this is just to illustrate the concept.

Alpha Mineron
  • 348
  • 1
  • 2
  • 13
  • Thank you very much – agr Jul 09 '17 at 17:56
  • @akshay Don't forget to mark it as the answer if it solved your issue – Alpha Mineron Jul 09 '17 at 17:58
  • Am new to stack overflow I donno how to mark answer.thank you for making me realize that such a thing exist.i will search and mark – agr Jul 09 '17 at 18:00
  • @akshay There's an uncolored tick, next to every answer below the two uncolored arrows which allow you to Upvote or downvote btw. You can click on the tick to make it green and mark an answer as selected. You can also Up vote answers so that they appear on top in an ordered manner. The most Up voted Comment is generally meant to show that it's helpful. By the way, I'd suggest you to do some research on this site before asking every doubt you have. There's a low tolerance towards low quality questions in the community. There are some good posts on the meta website of StackOverflow regarding it – Alpha Mineron Jul 09 '17 at 18:05
  • I tried upvoting for the answers I liked but it didn't allow bcoz I don't have some reputation it says,whatever I again didn't clearly get what that was.have to do research on this.and according to u I should be asking beginner questions in meta stack overflow ryt@ Alpha Mineron – agr Jul 09 '17 at 18:09
  • @akshay No, the meta website is a for the issues regarding what happens ON the main website(this). It's not for programming. Example: Lets say people are super mean to you and downvoted your question to like -10 and didn't even point out any mistake. Now there are already posts on meta regarding this so your post would be marked duplicated but assuming there aren't any posts, you could start a discussion on the meta website as how you can improve your question quality. You should also read the websites rules. – Alpha Mineron Jul 09 '17 at 18:17
  • @akshay You may read the questions on the meta website to get an idea of what I'm talking about! – Alpha Mineron Jul 09 '17 at 18:20
  • 2
    This answer is a bit misleading - needing to know the "function's address" has nothing to do with forward declarations/prototypes, it's merely a historical legacy. Consider that compiled languages like Java or C# work just fine without this. – Oliver Charlesworth Jul 09 '17 at 19:43
  • @OliverCharlesworth I'm sorry, could you explain a bit? How would the compiler know the functions details if no definition/prototype is mentioned before the call? – Alpha Mineron Jul 09 '17 at 20:03
  • @OliverCharlesworth You are on the wrong place buddy, look at the tag. We aren't talking about JAVA here. The question is on C++. – Alpha Mineron Jul 10 '17 at 01:31
  • Maybe my point is unclear - there is no intrinsic need for a prototype to appear first in order to compile, it's just how the C++ language is defined. – Oliver Charlesworth Jul 10 '17 at 07:43
  • @OliverCharlesworth So, you are saying that in general it's not required. Got it. But I don't think I should edit the answer as it's not related to the language that the OP is concerned about – Alpha Mineron Jul 10 '17 at 08:13
  • 1
    I'm not suggesting to talk about Java. I'm suggesting that the rationale in your answer is incorrect ;) – Oliver Charlesworth Jul 10 '17 at 17:29