0% found this document useful (0 votes)
2 views8 pages

Function in CPP

The document provides an overview of functions in C++, explaining their definition, advantages such as code reusability and optimization, and types including user-defined and library functions. It details parameter passing methods (pass by value and pass by reference), function declaration and definition, and the significance of the main function. Additionally, it covers recursion and friend functions, highlighting their unique characteristics and usage in C++ programming.

Uploaded by

technicalvcpt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views8 pages

Function in CPP

The document provides an overview of functions in C++, explaining their definition, advantages such as code reusability and optimization, and types including user-defined and library functions. It details parameter passing methods (pass by value and pass by reference), function declaration and definition, and the significance of the main function. Additionally, it covers recursion and friend functions, highlighting their unique characteristics and usage in C++ programming.

Uploaded by

technicalvcpt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

C++ Programming

Topperworld.in

Function

 A function is a set of statements that take inputs, do some specific


computation, and produce output.
 The idea is to put some commonly or repeatedly done tasks together and
make a function so that instead of writing the same code again and again
for different inputs, we can call the function.
 In simple terms, a function is a block of code that only runs when it is called.

 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:

return_type function_name(data_type parameter...)


{
//code to be executed
}

 Types of Functions

 User Defined Function

User Defined functions are user/customer-defined blocks of code specially


customized to reduce the complexity of big programs.
They are also commonly known as “tailor-made functions” which are built only
to satisfy the condition in which the user is facing issues meanwhile reducing
the complexity of the whole program.

 Library Function

Library functions are also called “builtin Functions“.


These functions are a part of a compiler package that is already defined and
consists of a special function with special and different meanings.
Builtin Function gives us an edge as we can directly use them without defining
them whereas in the user-defined function we have to declare and define a
function before using them.

©Topperworld
C++ Programming

 Parameter Passing to Functions

 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:

The result is: 35

©Topperworld
C++ Programming

 Ways to pass parameters:


 Pass by Value:
In this parameter passing method, values of actual parameters are copied to
the function’s formal parameters and the two types of parameters are stored
in different memory locations. So any changes made inside functions are not
reflected in the actual parameters of the caller.

 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:

// C++ Program to demonstrate function definition


#include <iostream>
using namespace std;

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

 Difference between call by value and call by reference

Call by value Call by reference

A copy of value is passed to the An address of value is passed to the


function function

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.

 Points to Remember About Functions

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

3. To declare a function that can only be called without any parameter, we


should use “void fun(void)“. As a side note, in C++, an empty list means a
function can only be called without any parameter. In C++, both void fun()
and void fun(void) are same.

 Main Function

The main function is a special function.


Every C++ program must contain a function named main. It serves as the entry
point for the program.
The computer will start running the code from the beginning of the main
function.

 Types of Main Functions

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

When function is called within the same function, it is known as recursion in


C++. The function which calls the same function, is known as recursive
function.
A function that calls itself, and doesn’t perform any task after function call, is
known as tail 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.

 Declaration of friend function in C++

Syntax :

class <class_name>
{
friend <return_type>
<function_name>(argument/s);
};

©Topperworld

You might also like