answersLogoWhite

0


Best Answer

Functions are short procedures or subroutines that can be called as and when required. Unlike goto statements, which branch off to a labelled section of code never to return, functions always return back to the caller. Functions may also call other functions. Functions are typically used to reduce the amount of duplicate code, producing more consistent and error-free code and reducing maintenance whenever functions need to be modified. Functions are also used to make code more readable by replacing complex or compound statements with a simpler function call. By using easy to understand function names, code becomes self-documenting, reducing the need to include verbose comments that only serve to interrupt the flow of the code.

Although there is a performance penalty in calling functions as opposed to inline code, the compiler's optimisation routines can inline expand functions whenever there is an advantage in doing so. Typically this applies to short functions with one or two simple statements, but can also apply to functions that are seldom called.

Functions may also return a value back to the caller (a function that does not return a value simply returns void). Functions can modify their behaviour according to arguments supplied to the function. The number and type of arguments is dependant upon the function signature. The signature also differentiates between functions with the same name, thus allowing functions to be overloaded to cater for different arguments types.

If the argument types are references (or pointers), the function can modify the arguments passed to it unless the arguments are declared const. Passing by reference also allows functions to return more than one value to the caller (typically the return value itself is used to provide diagnostics, such as an error code).

If the arguments are passed by value, however, the function creates a local copy of the argument, thus preventing changes to the argument that was actually passed. If the argument being passed is an object, the object's copy constructor is called automatically. Since this can be detrimental to performance, objects should always be passed by reference unless the function actually needs to work on a copy of the object rather than the original object.

In C++, classes make use of functions, known as member methods, to provide the implementation of operations that may be applied to an object's data. Classes may also employ virtual functions that allow derived objects to provide more specialised implementations whilst retaining the existing generic code. This also helps to reduce duplicate code, but also allows objects to behave polymorphically. That is, it is not necessary for a generic class to know any of the implementation details of its derivatives. You simply call the generic method of the base class and the virtual table redirects the call to the most-specialised override of that function, if one exists. If not, the generic method is called instead. Classes can also make use of pure-virtual functions, such that the base class need not provide any generic implementation at all. These classes are abstract classes which cannot be instantiated by themselves. Instead, you are expected to derive new classes from abstract classes, and the derived classes must provide the implementation even if the base class provides a generic implementation. Only classes that provide or inherit a complete implementation of all pure-virtual methods can actually be instantiated.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Every C++ program consists of one or more functions. At the very least, a C++ program requires an entry point, which is achieved by declaring a global function named main. The main function typically processes any command line arguments, acquires resources and invokes other functions. The program exits normally when a return statement is encountered within the main function or the end of the function is reached.

Whenever a function is called, execution immediately passes to that function. When a return statement is encountered or the end of the function is reached, control passes back to the caller. Thus a function can be thought of as being a subroutine -- a section of code that can be called and recalled whenever required. This makes it easy to break highly complex algorithms down into a series of small function calls.

Functions can also return a value to the caller. This is achieved by giving the function a type in the declaration, much as you would any other variable. This in turn means functions can be assigned to variables of the required type. If the return value is not assigned to a variable, the return value falls from scope and is lost forever. If a function does not need to return a value, it can be declared void -- which simply means no value is returned. The only function that cannot be declared void is the main function, which must return an int.

Functions can also accept arguments. This allows the same function to operate upon different values. Function arguments are local to the function and will fall from scope when the function returns. However, you can also use function arguments to return values (in addition to the return value). Arguments that return values are known as output arguments. In order to achieve this, the output argument must be declared as a reference or a pointer of the required type -- known as pass by reference. Arguments that do not return values are passed by value.

Values or references that are passed to functions are known as the actual arguments. Values or references received by a function are known as the formal arguments.

Class methods are also functions. These work just like ordinary functions but a hidden argument (the this pointer) is passed to these functions to indicate which instance of the class the function operates upon. Static methods do not have a this pointer. Both are scoped to the class in which they are declared.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A function is a code block that can be called from within another function (known as the calling function or simply the caller). When the called function returns, control is passed back to caller, and processing continues with the statement following the function call.

This allows code that appears in several places to be written just once, making the code much easier to read and (more importantly) reducing maintenance. With only one copy of the code, changes can be made in one place, rather than for every occurrence of the code, which may lead to errors.

Every program must have at least one function, int main(void), which defines the entry point of the application. Any non-trivial code that is repeated in several places within the main function is a suitable candidate for a function call. The same can be said of any function.

A function can alter its behaviour according to the variables passed to the function (known as the functions's arguments or its parameters). The number and type of parameters a function accepts differentiate between functions identified by the same name (known as function overloading).

Functions can also return a value to the caller, or can return void (meaning there is no return value). Multiple values can also be returned through one or more output parameters (pointer variables or references).

When passing variables by value, the value is automatically copied by the function. This allows the function to alter the copy without affecting the variable that was originally passed. While this is often desirable, particularly with primitive data types, passing complex objects incurs a copy overhead that will often be undesirable. Passing the object by reference passes the original variable without copying it. If the function does not intentionally alter the variable, the reference parameter should be declared const to prevent the function from making inadvertent changes to the referenced object.

Passing pointers to a function is similar to passing a reference, however, pointers are always passed by value. That is, the memory address contained in the pointer is copied, not the contents of that memory address. References are generally easier to work with as the object members can be accessed more directly, however if there's a possibility the object could be NULL, you must pass the object by pointer instead (references can never be NULL). Once the function has determined that the incoming pointer is non-NULL, the function can cast the pointer to a reference, thus simplifying the function code and eliminating the indirection overhead.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You understand a function by looking at its definition. The definition tells you exactly what a function does. The declaration (or the prototype) only tells you how to use the function. In other words, the declaration describes the interface to the function while the definition describes the implementation.

In binary libraries where you only have access to the header, the definition may not be available to you (it will be obfuscated within the binary code). In that case you must look up the library documentation in order to understand the function.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

actually ++ in c++ represents the advanced version of the language 'c'. Whereas c dont has any full form. In c ,c ++ means c =c +1

I think the question goes more to what "function definition" means. C++ (and many other languages) requires that that a function (any symbol really) must be known to the compiler before you actually try to use it. This is called the "function declaration" and simply specifies the signature of the function. The "function definition" then is the actual implementation of the function.

You can declare and implement a function at the same time but normally you will split your source code into a header file (.h) containing the declarations and an implementation file (.cpp) containing the the definitions. For example:

Declaration:

class A

{

....

void doSomething();

}

Definition:

void A:doSomething()

{

// Your implementation here

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A function in C++ is a procedure -- a group of statements that perform a specific task and that can be called as often as required. Functions may accept one or more parameters which allows the function to alter its behaviour according to those parameters. Functions can also return a (single) value to the caller. If multiple values must be returned, these can returned by using output parameters (references or pointers), or the return value itself could be a structure or class containing the return values.

Functions can also call other functions. Since calling a function requires that the current function must stop everything its doing, a special structure known as the call stack is used to facilitate the call. The call stack effectively keeps track of the caller's local variables, allows the caller to pass values to the callee and allows the callee to return control back to the caller through a return address. By pushing values in a specific sequence and popping them in the reverse sequence, the call stack essentially provides an automatic "breadcrumb" trail with the absolute minimum information required.

The call-process is a bit more complicated than that because CPU registers are also used to pass values to the callee, including the return address and the next instruction address. Thus the call stack is used primarily to store the caller's local variables. However, there are only a limited number of registers available, so the call stack is also used for any callee parameters that cannot be placed in the registers -- technically known as a "spill".

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

In C++, a function is a procedure call, a subroutine that allows otherwise procedural code to branch off to a named code section, execute it, and return to the point it left off. Functions can optionally return a value directly to the caller, typically used to determine the result of the function call, such as an error code. However, functions can also be parametrised to allow the caller to alter the behaviour of the function, and can use those parameters for both input and output purposes, thus allowing a function to return several values over and above the return value itself.


Functions can be thought of as being mini-programs that can be called upon as often as is required. In other words, they contain re-usable code. Thus, if you have a section of code that appears repeatedly throughout your code, then it is better to simply turn that code into a function and call it wherever it is required rather than repeatedly type out the same group of statements over and over (which may lead to typographic errors). However, functions can also simplify your code making it easier to read. So even if a complex line of code is only called once in your code, if a function name alone would help the reader follow the logic of the code better than a more verbose comment would, then it's often worth placing that code in a function.


Most functions are fairly simple, perhaps just fetching a value from a memory address or storing a value, but they can be as complex as desired, whether it is to perform a mathematical operation or to group a series of repeated function calls as a separate function. Remember that even if you have no other functions other than the built-in functions, you must always have a main() function which defines the entry-point to your application. And although you could write an entire program within this one function, if it were non-trivial then it would quickly become unreadable and difficult to maintain.


Of course, there is a cost involved in calling functions. However, the compiler's optimisers will automatically eliminate many function calls, especially those with perhaps one or two lines of actual code, and those that are seldom called. This is known as inline expansion. Those functions still exist in your source code, of course, but your code is not only easier to maintain (there's only one copy of each function so you don't have to update multiple instances of the same code, which often leads to coding errors), and your code that much more readable.


While there is a cost involved in calling functions, for those functions that simply cannot be inline expanded because they are either overly complex or are called far too many times, the cost in making the function call more than offsets any performance that would be lost through the increased code size that inline expansion inevitably incurs.


This answer is:
User Avatar

User Avatar

Wiki User

14y ago

a function is a code in braces to reduce the complexity of the program.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

function is a subprogram that acts on a data and often returns a value

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is function in C plus plus programming language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you convert numeric value into alpha value in c plus plus programming language?

use the _itoa function


What is swap in c plus plus programming language?

It is not a reserved word, so can be an identifier (name of a type/variable/function).


Who is the owner of c plus plus language?

Bjarne Stroustrup is the author of C++. However, no one "owns" this language.


What is the significance of c plus plus?

C++ is an object oriented programming language


What is the use of c plus plus in banks?

Programming language.


Is combination a library function in c language of programming?

No.


Differentiate C plus plus and Turbo C?

Turbo C is a software where C or C++ programming environment resides in.But C++ is itself a programming language.


Who is founder of C plus plus programming language?

Bjorn Stroustrup


Is c plus plus an unstructured programming language?

No, BASIC is, for example.


Is score a reserved word in the C plus plus programming language?

No.


Who designed the C plus plus programming language?

Bjarne Stroustrup


What is programming languages in c plus plus?

Programming in C++ means designing and writing a computer program using the C++ programming language. C++ is a high-level, machine-independent language which must be converted into machine-dependent code by a compiler.