C++ Functions
C++ Functions
The function in C++ language is also known as procedure or subroutine in other programming
languages.
To perform any task, we can create function. A function can be called many times. It provides
modularity and code reusability.
Advantage of functions
- Code Reusability
By creating functions in C++, you can call it many times. So we don't need to write the
same code again and again.
- Code optimization
It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (531, 883 and 781) whether it is prime number
or not. Without using function, you need to write the prime number logic 3 times. So,
there is repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it
several times.
Types of Functions
There are two types of functions in C programming:
- Library Functions
These are the functions which are declared in the C++ header files such as ceil(x),
cos(x), exp(x), etc.
- User-defined functions
These are the functions which are created by the C++ programmer, so that he/she can
use it many times. It reduces complexity of a big program and optimizes the code.
Examples, greeting(), summation(int a, int b) and other that you will create.
Page 1 of 5
Parts of a function
✓ A function declaration tells the compiler about a function's name, return type, and
parameters.
✓ A function definition provides the actual body of the function.
• Return Type − A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
• Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
• Parameters or Arguments − A parameter or argument is like a placeholder. When a
function is invoked/called, you pass a value to the parameter. This value is referred to
as actual parameter or argument.
The parameter list refers to the type, order, and number of the parameters of a
function. Parameters are optional; that is, a function may contain no parameters.
Page 2 of 5
• Function Body − The function body contains a collection of statements that define
what the function does
Function Declarations
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
A function declaration has the following parts −
return_type function_name( parameter list );
For the above defined function max(), following is the function declaration −
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so
following is also valid declaration −
int max(int, int);
Function declaration is required when you define a function in one source file and you call
that function in another file. In such case, you should declare the function at the top of the
file calling the function.
Example 1
A program to create a user defined function called greeting function that print a “Hello
there” when called in the main function.
Page 3 of 5
Observations:
Example 2
A program with a function will accepts an integer number and print whether the number is
even or not even.
Observations
Page 4 of 5
Example 3
A program with the functions that prints the sum and the difference of the two numbers.
Task to do
Page 5 of 5