text: Hey everybody,it's the c++ creator.I am a beginner at C++,and am getting confused at functions.I am learning from a site called cprogramming.com and this is what they say-
  Lesson 4: Functions
  (Printable Version):
  #include <cstdlib> // Include rand()
  using namespace std; // Make rand() visible
  int a = rand(); // rand is a standard function that all compilers have
  Do not think that 'a' will change at random, it will be set to the value returned when the function is called, but it will not change again.
  The general format for a prototype is simple:
  return. Let's look at a function prototype:
  int mult ( int x, int y );
  This prototype specifies that the function mult will accept two arguments, both integers, and that it will return an integer. Do not forget the trailing semi-colon. Without it, the compiler will probably think that you are trying to write the actual definition of the function.
  When the programmer actually defines the function, it will begin with the prototype, minus the semi-colon. Then there should always be a block with the code that the function is to execute, just as you would write it for the main function. Any of the arguments passed to the function can be used as if they were declared in the block. Finally, end it all with a cherry and a closing brace. Okay, maybe not a cherry.
  Let's look at an example program:
  ;
  } as a prototype as well.
  Return is the keyword used to force the function to return a value. Note that it is possible to have a function that returns no value. If a function returns void, the return statement is valid, but only if it does not have an expression. In other words, for a function that returns void, the statement "return;" is legal, but redundant.
  The most important functional (Pun semi-intended) question is why do we need a function? Functions have many uses. For example, a programmer may have a block of code that he has repeated forty times throughout the program. A function to execute that code would save a great deal of space, and it would also make the program more readable. Also, having only one copy of the code makes it easier to make changes. Would you rather make forty little changes scattered all throughout a potentially large program, or one change to the function body? So would I.
  Another reason for functions is to break down a complex program into logical parts. For example, take a menu program that runs complex code when a menu choice is selected. The program would probably best be served by making functions for each of the actual menu choices, and then breaking down the complex tasks into smaller, more manageable tasks, which could be in their own functions. In this way, a program can be designed that makes sense when read. And has a structure that is easier to understand quickly. The worst programs usually only have the required function, main, and fill it with pages of jumbled code.
  Could anybody explain this?
  Thanks in advance,
  c++ creator
  Is there anything specific you do not understand as there is a lot of information from
  the beginning
  to
  the end.
  Possibly explain what you do understand and then we can help with the rest. Otherwise what we would need to do is create a response that is nothing but a complete tutorial of what a function is.
  No,like what is a prototype?You would not believe this,but I am just 10 years old and I am trying to program.It's true.Just because I like to code,I know a quite a lot of HTML too.Reply.
  c++ creator
  Hey, that's cool. Doesn't matter if your 10 or 50, we all have the same goal here. We are here to learn, so kudos to you.
  A prototype that they are discussing above is the data type or void that is prior to the function name.
  void test();
  void is the prototype and test is the function name. This is the only function that does not require a -return- in the function definition.
  int time();
  int is the prototype (data type) and time is the function name. This, like all other data types, requires a return statement in the function definition. Such as this next example.
  int time(){ // function definition, { is the start of the block of code int minutes = 60; int seconds = 60; int hours = minutes * seconds; return hours; } // end of block of code, end of function definition // hours is of type int. The function, int time(), requires the return to be of int value. If hours was a type double, float, or any non integer then the compiler would likely give an error.
  Functions are made for code that is anticipated to be frequently used. Later on you will learn about classes and functions. That's where the fun begins.
  Are you a Youtube user?If then could you create a tutorial on functions?
  Correction: The function prototype is that which is declared above before you even start the actual coding, before the main function even. It's used as a reference to the actual function later to kind of double check if everything is in order.
  Thanks Saith and Cross213,though what does Cross213 mean by
  It's used as a reference to the actual function later to kind of double check if everything is in order.
  ?
  Thanks Saith and Cross213,though what does Cross213 mean by?
  He means (I hope) that the prototype introduces enough information about a function to safely call it without requiring the function's definition to be visible.
  Sorry, sorry. Cross213 is right. I misread it. I usually use the word "declaration" instead of "prototype".
  int time(); // is the declaration or prototype of the function. This needs to be stated prior to the definition of the function. Functions need to be declared prior to the main() function.
  .. example ..
  int time(); // prototype, or declaration of a function. Basically declaring the use of this function -prior- to it being -called- in the main() function. int main(){ //block of code return 0; } int time(){ // function definition, { is the start of the block of code int minutes = 60; int seconds = 60; int hours = minutes * seconds; return hours; } // end of block of code, end of function definition. This function is still has the data type of int, so an int is still required as the return type.
  As Narue was stating, 'He means (I hope) that the prototype introduces enough information about a function to safely call it without requiring the function's definition to be visible. "
  Later on, you are allowed to have the -function definition- in another file, but the declaration (prototype) needs to be in the current file if you plan on using that function.
  Functions need to be declared prior to the main() function.
  To be precise, functions need to be declared before they can be called.
  You're so helpful, Narue :D
  c++ creator is now cadence441.See my status,it shows I am banned!What wrong did I do?I wasn't even able to send a message to somebody.
  You're banned because you're underage. Please do not post again or I'll ban cadence441 as well.
  I didn't realise there was an age limit. Could you let me know what it is. I'm 3 years old by the way.
  I didn't realise there was an age limit. Could you let me know what it is. I'm 3 years old by the way.
  While I'll admit that I'd overlooked this myself, the age limit is spelled out in the Acceptable Use Policy:.
  Looking up the Children Online Privacy Protection Act (COPPA), it would be a violation to collect details such as names and locations for individuals under the age of 13 without written parental permission, or at least that's what I understand it to say - I don't have my Legalese to English dictionary on hand right now. It seems to me that, while there would be ways to accommodate underage members, the trouble needed to allow for it would be far too much to deal with feasibly.
  I'll be back in a decade.