01_02_Function
01_02_Function
2. Func Overloading
3. Inline function
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.
Return Values
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 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.
add(int a, int b)
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.
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++
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 (*).
References as aliases
2.References as aliases
Reassignment
return type
argument list
Void
Functions that do not return a value and describes an empty collection of values.
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.
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
Simple example:
OP :
OP
The value of *ptr1 :
90