1.
Function(parameters and arguments)
2. Func Overloading
3. Inline function
4. Call by value, reference, pointer
5. Function prototype
6. Pointer
7. Array of pointors
C++ Functions
block of code which only runs when it is called.
call by user explicitly.
You can pass data, known as parameters.
Functions are used to perform certain actions, and they are important for reusing
code: Define the code once, and use it many times.
pre-defined function main(), which is used to execute code.
To perform any task, we can create function. A function can be called many times.
It provides modularity and code reusability.
void means that the function does not have a return value.
Parameters and Arguments
Information can be passed to functions as a parameter.
Parameters act as variables inside the function.
Why Do We Need Functions?
Arguments typically refer to the values or expressions that you provide to a
function when you call it. Functions can take zero or more arguments, and these
arguments are used to pass data to the function for processing.
In this example, int a and int b are the arguments.
5 and 3 are the arguments
Return Values
void function should not return a value.
If you want the function to return a value, you can use a data type (int, string, etc.)
instead of void, and use the return keyword inside the function:
Pass Arrays as Function Parameters
o/p: 1 2 3 4 5
Functions make code modular.
Functions provide abstraction. For example, we can use library functions without
worrying about their internal work.
Function Overloading
where two or more functions can have same name but different parameters.
“Function” name should be the same and the arguments should be
different.
Function overloading is a form of compile-time (static) polymorphism,
also known as ad hoc polymorphism.
Function overloading can be considered as an example of a polymorphism
feature in C++
add(int a, int b)
add(double a, double b).
Parameters should have a different type, different number, different sequence
of parameters.
Inline Functions in C++
reduce the function call overhead
(which can impact the program's execution time and memory usage).
An inline function may increase efficiency if it is small.
keyword inline is used to declare a function as inline.
When you define a function as inline, you are providing a hint to the compiler,
suggesting that it should attempt to replace function calls with the actual code of the
function, thereby potentially improving performance.
Call by value and call by reference in C++
There are two ways to pass value or data to function in C language: call by value and
call by reference. Original value is not modified in call by value but it is modified in
call by reference.
Call by value in C++
In call by value, original value is not modified.
This method copies the actual value of an argument into the formal parameter of the
function.
Call by reference in C++
In call by reference, original value is modified because we pass reference (address).
This method copies the reference of an argument into the formal parameter. Inside
the function, the reference is used to access the actual argument used in the call. This
means that changes made to the parameter affect the argument
Reference is a variable that behaves as an alias for another variable.
What is Reference?
A reference is a variable that is referred to as another name for an already
existing variable. The reference of a variable is created by storing the address of
another variable.
A reference variable can be considered as a constant pointer with automatic
indirection. Here, automatic indirection means that the compiler automatically applies
the indirection operator (*).
How to create a reference?
Reference can be created by simply using an ampersand (&) operator.
C++ provides two types of references:
References to non-const values
References as aliases
1.References to non-const values
2.References as aliases
Reassignment
The reference variable cannot be modified.
Swap of 2 nos using reference
Call by Pointer
This method copies the address of an argument into the formal parameter. Inside the
function, the address is used to access the actual argument used in the call. This
means that changes made to the parameter affect the argument.
C++ Reference vs Pointer
A reference is a variable which is the pointer is variable that stores
another name of the existing the address of another variable.
variable.
We cannot reassign the reference
variable
C++ reference and pointer seem to be similar,
but there are some differences that exist between them.
A reference is a variable which is another name of the existing variable,
while the pointer is variable that stores the address of another variable.
Function Prototype in C++
A function prototype is a declaration of the function that informs the program about
the number and kind of parameters, as well as the type of value the function will
return.
components of a function prototype:
return type
name of the function
argument list
Void
Functions that do not return a value and describes an empty collection of values.
no parameters and has an empty argument list
OP Welcome to JavaTPoint
OP 5
C++ Recursion
When function is called within the same function, it is known as recursion in C++.
A function that calls itself, and doesn't perform any task after function call, is known
as tail recursion.
In tail recursion, we generally call the same function with return statement.
Let's see a simple example of recursion.
recursionfunction()
{
recursionfunction(); //calling self function
}
What is Pointer?
A pointer is a variable that contains the address of another variable.
It can be dereferenced with the help of (*) operator
Pointer variable that stores the memory address of another variable.
Void Pointers that point to a value that has no type are known as void pointers.
Usage of pointer
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc()
functions where pointer is used.
2) Arrays, Functions and Structures
Pointers in c language are widely used in arrays, functions and structures. It
reduces the code and improves the performance.
What is a pointer to a pointer?
C++ Array of Pointers
The name of an array is considered as a pointer
C++ considers the array name as the address of the first element.
For example, if we create an array, i.e., marks which hold the 20 values of integer
type, then marks will contain the address of first element, i.e., marks[0].
Therefore, we can say that array name (marks) is a pointer which is holding the
address of the first element of an array.
Array of Pointer to Strings
OP :
John
Peter
Marco
Devin
Ronan
C++ Void Pointer
Syntax
Simple example:
OP :
100 Same Address
100
Difference between void pointer in C and C++
ptr1 without any typecasting
because in C,
we do not need to typecast
while assigning the void pointer
to any other type of pointer.
OP
The value of *ptr1 :
90
we want to assign the void
pointer to integer pointer,
in order to do this, we need
to apply the cast
operator, i.e., (int *) to the
OP
The value of *ptr1 is :
10