In this lesson I will show you how to write and call a function in order to simplify the structure of any program.
Creating a function:
A function in C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:
Type name (parameter1, parameter2,) {statements}
Where:
–type is the type of value returned by the function.
–name is the identifier by which the function can be called.
–parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: Int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
– Statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.
Types of functions:
System-Supplied Functions:
These are available to anyone who writes a C++ program. This saves the programmer’s time from writing own functions. They are completely debugged, efficient and always produce a precise output. The important contribution of the system-supplied functions is that they provide clarity to the program because they do not have to be redefined. It reduces the source code, which saves time of the programmer.
User-Supplied Functions:
C++ language allows additional functions besides the built-in functions called the user-defined function. It allows programmers to define their own functions. The programmer must code the logic of this type. In order to do so, a declaration is needed.
Calling a function:
While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.
This is an example of a code where I created a function and then I called it into my main.
//Function that calculates the area of a rectangle #include <iostream> using namespace std; //Declaration of what the function does void area(){ int l=0, w=0; cout<<"Please enter length and width: "<<endl; cout<<"Length: ";cin>>l; cout<<"Width: ";cin>>w; cout<<"The area is: "<< l * w<<endl; } int main(){ cout<<"I will calculate the area of a rectangle"<<endl; area();//Calling the function inside the execution of the program }
And this is how the program runs:
Any doubt please leave it in comments and I will be glad to answer.
-The admin.
Creating C++ functions and calling them. by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.