Function in CPP
Function in CPP
Topperworld.in
Function
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.
Function Declaration
A function declaration tells the compiler about the number of parameters
function takes data-types of parameters, and returns the type of function.
Putting parameter names in the function declaration is optional in the function
declaration, but it is necessary to put them in the definition.
©Topperworld
C++ Programming
Syntax:
Types of Functions
Library Function
©Topperworld
C++ Programming
Actual Parameters
The parameters passed to function are called actual parameters. For example,
in the program below, 5 and 10 are actual parameters.
Formal Parameters
The parameters received by the function are called formal parameters. For
example, in the above program x and y are formal parameters.
Example:
#include <iostream>
// Function with formal parameters
int multiply(int x, int y) {
return x * y;
}
int main() {
// Actual parameters
int a = 5;
int b = 7;
// Call to the function with actual parameters
int result = multiply(a, b);
std::cout << "The result is: " << result << std::endl;
return 0;
}
Output:
©Topperworld
C++ Programming
Pass by Reference:
Both actual and formal parameters refer to the same locations, so any changes
made inside the function are actually reflected in the actual parameters of the
caller.
Example:
// C++ program to implement pass-by-reference
#include <iostream>
using namespace std;
void f(int & x)
{
x--;
}
// Driver code
int main()
{
int a = 5;
cout << a << endl;
f(a);
cout << a << endl;
}
©Topperworld
C++ Programming
Function Definition
Pass by value is used where the value of x is not modified using the function
fun().
Example:
void fun(int x)
{
// definition of
// function
x = 30;
}
int main()
{
int x = 20;
fun(x);
cout << "x = " << x;
return 0;
}
Output:
x = 20
©Topperworld
C++ Programming
Changes made inside the function is Changes made inside the function is
not reflected
reflected on other functions outside the function also
Actual and formal arguments will be Actual and formal arguments will be
created in created in
different memory location same memory location.
1. Most C++ program has a function called main() that is called by the
operating system when a user runs the program.
2. Every function has a return type. If a function doesn’t return any value, then
void is used as a return type. Moreover, if the return type of the function is
void, we still can use the return statement in the body of the function definition
by not specifying any constant, variable, etc. with it, by only mentioning the
‘return;’ statement which would symbolize the termination of the function as
shown below:
Example:
void function name(int a)
{
....... // Function Body
return; // Function execution would get terminated
}
©Topperworld
C++ Programming
Main Function
1. Without parameters:
// Without Parameters
int main() { ... return 0; }
2. With parameters:
// With Parameters
int main(int argc, char* const argv[]) { ... return 0; }
C++ Recursion
©Topperworld
C++ Programming
In tail recursion, we generally call the same function with return statement.
Syntax:
recursionfunction()
{
recursionfunction(); // calling self function
}
Friend Function
A friend function is a special function in C++ which in-spite of not being
member function of a class has privilege to access private and protected
data of a class.
A friend function is a non member function or ordinary function of a
class, which is declared as a friend using the keyword “friend” inside the
class. By declaring a function as a friend, all the access permissions are
given to the function.
The keyword “friend” is placed only in the function declaration of the
friend function and not in the function definition.
When friend function is called neither name of object nor dot operator
is used. However it may accept the object as argument whose value it
want to access.
Friend function can be declared in any section of the class i.e. public or
private or protected.
Syntax :
class <class_name>
{
friend <return_type>
<function_name>(argument/s);
};
©Topperworld