The process of writing a function in C++ is very simple. Actually it consists of the following parts :
- Function header
- Function body
Syntax
The syntax of writing a function is as follows:
return-type function-name(parameters)
{
statement(s);
}
Function header
The first line of a function definition is known as a function header(function definition will be discussed later in this blog).
Its syntax is as follows:
Syntax
return-type function-name(parameters)
Function Body
The set of statement(s) written inside curly braces are known as function body. These are actually the statement(s) that are executed inside the function.
Function declaration
Function declaration is also known as function signature or function prototype.Remember that it ends with a semi colon. It tells the compiler about the structure of the function to be used in the program. It is written before the main( ) body.Its syntax is as follows :
Syntax
return-type function-name(parameters)
Example
void display( )
Function definition
A set of statements that actually explains what a function does is called a function. Remember that function definition can be written at the following places:
- Before main( ) function
- After main( ) function
- In a separate file
Syntax
return-type function-name(parameters)
{
statement(s);
}
Example
void display( )
{
cout<<"Hello world";
}
No comments:
Post a Comment