Function:
- A function is an independent module that performs certain calculations or functions, and is one of the most basic of programming.
- You can write efficient programs by modularizing the functions into independent units and defining functions, and calling these functions whenever you need them.
- The structure of the function is as follows.
'(1) data type' '(2) function name ' ( ' (3) parameters' )
{
'(4) execution sentences';
'(5) return' value ';
}
'(4) execution sentences';
'(5) return' value ';
}
(1) Data type: Data type of return value. For example,
if you want to return an integer value, you can write an int here. If there is
no return value, you can write void.
(2) Function name: It is the part to write the name of
the function.
(3) Parameters: The arguments to be used in the
function such as variables, constants, and classes. Of course, some functions
do not have arguments.
(4) Execution sentences: A collection of execution statements that perform
specific functions.
(5) return' value: A statement that returns the result
of the function. The returned value may not be present.
- Elements of a function
* Declaration of a function (prototype)
* Definition of a function
* Calling a function
(1) The declaration of a function: It declares the data type, function
name, and input arguments of the returned value to be used in the program
before defining the function in the program.
Since programs are compiled sequentially, the function is called first in
the program, and an error occurs if the function be defined later.
If you declare a function in advance, calling any undefined functions is
not a problem.
(2) The definition of a function: It is to specifically specify what
function to perform.
(3) A call to a function: If it is needed within a program, executes a
function created at a specific location.
Let's try a simple example.
Example Code
#include
<iostream>
using
namespace std;
void
ff_1()
{
cout
<< " Hi ! " << endl;
}
int
main() {
ff_1();
return 0;
}
- The function named 'ff_1' with no return value and no
arguments was defined. If the function is called, a sentence "Hi!" will
be printed. When you run it, "Hi!" is printed.
result:
" Hi !
- Let's take a look at what happens if we define the function after the 'main' function.
- In the above code, if you change the order of 'main' and 'ff_1' function as
follows, you can see that a compile error occurs.
#include
<iostream>
using
namespace std;
int
main() {
ff_1();
return 0;
}
void
ff_1(){
cout
<< " Hi ! " << endl;
}
- The 'ff_1' function is called in the 'main' function.
The 'ff_1' function is defined later, and the following error message occurs
at compile time.
ch4.c:
In function ?int main()??
ch4.c:6:
error: ? 'ff_1??was not declared in this scope
- Function 'ff_1' is not declared. You can solve this
problem by putting the prototype of the function before the 'main' function as
follows.
#include
<iostream>
using
namespace std;
void
ff_1();
int
main() {
ff_1();
return 0;
}
void
ff_1()
{
cout
<< " Hi ! " << endl;
}
No comments:
Post a Comment