Functions and Pointers
Functions and Pointers
A function is a set of statements that takes input, does some specific computation, and produces
output. The idea is to put some commonly or repeatedly done tasks together to make a function so
that instead of writing the same code again and again for different inputs, we can call this function.
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.
Friend Function
A friend function is a special function in C++ which in spite of not being a member function of a
class has the privilege to access private and protected data of a class.
A friend function is a non-member function or an ordinary function of a class, which is declared by
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 but not in the function definition.
When the friend function is called neither the name of the object nor the dot operator is used.
However, it may accept the object as an argument whose value it wants to access.
A friend function can be declared in any section of the class i.e. public, private, or protected.
Pass by Value
In Pass by Value method, a variable’s actual value is copied and then passed to the function
instead of the original variable. As the result, any changes to the parameter inside the function will
not affect the variable’s original value outside the function.
Pass by Reference
In pass-by-reference method, instead of passing the argument itself, we passes the reference of
an argument to the function. This allows the function to change the value of the original argument.
// driver code
int main()
{
int x = 5;
int y = 10;
cout << "Before swap:\n";
cout << "x = " << x << ", ";
cout << "y = " << y << endl;
// driver code
int main()
{
int x = 5;
int y = 10;
return 0;
}
Call By Value vs Call By Reference
In call-by-values, we cannot alter the values In call by reference, we can alter the values of
of actual variables through function calls. variables through function calls.
Values of variables are passed by the Pointer variables are necessary to define to store
Simple technique. the address values of variables.
Call by value is considered safer as original Call by reference is risky as it allows direct
data is preserved. modification in original data.
Remember, inlining is only a request to the compiler, not a command. The compiler can ignore the
request for inlining.
C++ Pointers
A Pointer in C/C++ is a special type of variable that stores the memory address of another variable.
Instead of holding a value directly, a pointer "points" to where a value is stored in memory.
In C and C++, pointers have their own data types, which are determined by the type of the variable
they point to. The type of a pointer specifies the type of data the pointer is intended to point to. Here
are some examples:
Dangling Pointer in C
A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer.
Void Pointer in C
Void pointer is a specific pointer type – void * – a pointer that points to some data location in storage,
which doesn’t have any specific type. Void refers to the type. Basically, the type of data that it points to
can be any. Any pointer type is convertible to a void pointer hence it can point to any value.
Note: Void pointers cannot be dereferenced. It can however be done using typecasting the void
pointer. Pointer arithmetic is not possible on pointers of void due to lack of concrete value and
thus size.
NULL Pointer in C
NULL Pointer is a pointer that is pointing to nothing(i.e. not pointing to any valid object or memory
location). In case, if we don’t have an address to be assigned to a pointer, then we can simply use
NULL. NULL is used to represent that there is no valid memory address.
Note NULL vs Uninitialized pointer – An uninitialized pointer stores an undefined value. A null
pointer stores a defined value, but one that is defined by the environment to not be a valid
address for any member or object. NULL vs Void Pointer – Null pointer is a value, while void
pointer is a type
A NULL pointer explicitly points to no valid memory address, whereas an uninitialized pointer
contains an unpredictable memory address.
A NULL pointer explicitly points to no valid memory address, while a void pointer is a type of
pointer that can point to any data type but doesn't have a specific type itself.
NULL Pointer: A special value used to indicate that the pointer is not currently pointing to any
valid memory location. This is useful for initialization and for error checking before dereferencing
pointers.
Value of NULL : Represents a pointer that does not point to any valid memory location, usually
defined as zero (0).
Wild pointer in C
A pointer that has not been initialized to anything (not even NULL) is known as a wild pointer. The
pointer may be initialized to a non-NULL garbage value that may not be a valid address.
Syntax
dataType *pointerName;
Applications of Pointers in C
1. Passing Arguments by Reference
i. to modify the variable in another function.
ii. For Efficiency Purpose: Passing large structure without reference would create a copy of the
structure (hence wastage of space).
2. For Accessing Array Elements
3. To Return Multiple Values
4. For dynamic memory Allocation
Memory It shares the same address as the Pointers have their own memory
Address original variable. address.
Null Value It does not have null value. It can have value assigned as null.
This variable is referenced by the The pointer does it work by the method
Arguments
method pass by value. known as pass by reference.
Important
When to use What
The performances are exactly the same as references are implemented internally as pointers. But still,
you can keep some points in your mind to decide when to use what:
Use references:
Use pointers:
If pointer arithmetic or passing a NULL pointer is needed. For example, for arrays (Note that
accessing an array is implemented using pointer arithmetic).
To implement data structures like a linked list, a tree, etc. and their algorithms. This is so because,
in order to point to different cells, we have to use the concept of pointers.
Accessing The value of the arguments is accessed The reference name can be used
Values via the dereferencing operator *. to implicitly reference a value.
Parameters Pass by Pointer Pass by Reference
A pointer can be re-assigned while a reference cannot, and must be assigned at initialization only.
The pointer can be assigned NULL directly, whereas the reference cannot.
Pointers can iterate over an array, we can use increment/decrement operators to go to the
next/previous item that a pointer is pointing to.
A pointer is a variable that holds a memory address. A reference has the same memory address
as the item it references.
A pointer to a class/struct uses ‘->’ (arrow operator) to access its members whereas a reference
uses a ‘.’ (dot operator)
A pointer needs to be dereferenced with * to access the memory location it points to, whereas a
reference can be used directly.
References in C++
When a variable is declared as a reference, it becomes an alternative name for an existing variable. A
variable can be declared as a reference by putting ‘&’ in the declaration.
References vs Pointers
1. A pointer can be declared as void but a reference can never be void For example
int a = 10;
void* aa = &a; // it is valid
void& ar = a; // it is not valid
1. The pointer variable has n-levels/multiple levels of indirection i.e. single-pointer, double-pointer,
triple-pointer. Whereas, the reference variable has only one/single level of indirection.
2. Reference variables cannot be updated.
3. Reference variable is an internal pointer.
4. Declaration of a Reference variable is preceded with the ‘&’ symbol ( but do not read it as
“address of”).
int main()
{
// Ideally, it should have called fun(char *),
// but it causes compiler error.
fun(NULL);
}
Output: