answersLogoWhite

0


Best Answer
What is a function?

A function is a procedure call; a re-usable block of one or more statements that can be called from any code that has access to it. Unlike a goto or a jump statement, functions automatically return to the caller when the function has completed its given task, just as if the code within the function were placed directly into your code (known as inline expansion). Functions are most useful whenever the same statements need to be called over and over from several places in your code, but without the need to duplicate that code (thus reducing maintenance costs). And since functions can call other functions, they can also be used to reduce highly complex functions into much smaller, easier to read functions.

Functions also differ from goto and jump statements in that they can be parametrised. That is, arguments can be passed to the function to alter its behaviour according to a given set of parameters. Moreover, functions can also return a value to the caller. The return value is often used to return an error code to indicate the success or failure of the function, but can also be used to return any one value. If more than one value must be returned, you can either return a complex object (a class, struct or union) or you can use output parameters (parameters may be used to provide input to a function, output from a function, or indeed both).

Parameters and arguments

When discussing functions, you will often encounter the terms parameters and arguments, often prefixed with the terms formal and actual. While many languages do not make any particular distinction between the terms, let's clarify exactly what these terms mean in the general sense.

An argument is an actual parameter or actual argument that is passed to a function from the calling code. In other words, it is the actual variable or constant that you pass into a function.

A parameter is a formal parameter or formal argument that always appears in a function's declaration and/or definition, and is used to accept the arguments passed to it. That is, the arguments are assigned to the parameters. Parameters are often declared but not named within function declarations, but they must be named in function definitions if the function's definition requires access to that parameter. Parameters that are declared but not named in a definition cannot be used by the definition, and are often called dummy parameters.

Function Signatures

The name of a function and the parameters it accepts determine the function's signature. The function signature is used to disambiguate functions that have the same name but different parameters, known as function overloading. The return type of a function does not form any part of its signature, thus overloaded functions cannot differ by return type alone.

Types of Functions

In C++ there are essentially just two major types of function: external functions and member functions. A member function is also known as a method or member method, and is always declared to be a member of a class. Access to the method is determined by the class in which it is declared. External functions do not belong to any class and can be called from any function or class that has access to it.

Methods can also be classified by what they do. Accessor functions are used to retrieve information from the class while mutators manipulate the information within the class. Operators are methods that perform operations upon the information (such as assignment and equality). Operators can also be used to dynamically cast objects to different classes of object or even primitive data types. Special methods known as constructors initialise the class while a destructor cleans up the class when it falls from scope. Unlike other methods and functions, neither constructors nor destructors return any value, not even void. Methods can also be further classified as being non-virtual, virtual and pure-virtual in order to provide any combination of concrete and abstract interfaces to the class.

External functions usually provide auxiliary or utility functionality to a program. Such functions are usually miscellaneous in terms of their functionality and although some could be combined and classified to become member methods, the additional work required to design a class around them will often outweigh the usefulness in having the class. However, if the external functions require access to global data, it is better to combine those functions and the global data into a class, if only to eliminate the need for global data (which is generally a good thing).

C++ also includes many built-in functions made available though the standard library, including template classes and functions. Combined with user-defined classes and functions, these greatly reduce the need to design your own classes and functions entirely from scratch.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a function and its types in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A c plus plus statement that invokes a function is known as?

...a function call.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


Basic structure of c n c plus plus?

The basic structure of a C or C++ program is built around types. A structure is a type. A function is a type. A class is a type. All of these types can be built from primitive (built-in) types and can be used to create ever-more complex types.


Why to use gotoxy function in c plus plus?

to locate coordinates ..

Related questions

What are the building function in c plus plus?

There is no such term as "building function" in C++.


A c plus plus statement that invokes a function is known as?

...a function call.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


When will you make a function inline in c plus plus?

yes,we can make function inline


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


What do you call an object function in c plus plus?

method


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


Basic structure of c n c plus plus?

The basic structure of a C or C++ program is built around types. A structure is a type. A function is a type. A class is a type. All of these types can be built from primitive (built-in) types and can be used to create ever-more complex types.


What is the function y equals ax2 plus bx plus c?

It is a quadratic function which represents a parabola.


How do you create folder with c plus plus?

Use function mkdir.


Why to use gotoxy function in c plus plus?

to locate coordinates ..