C++ Passing Pointers to Functions3 min read

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument to be passed while calling a function then it is called actual arguments. There are two ways to call a function:

  • call by value
  • call by reference

In a first way, we pass the actual values as an actual argument and those values are copied to the function and the function does some task with them. And the second way to call a function can be processed in two ways:

  • by passing the references
  • by passing the pointers

In C++ programming, just like we pass values of the variable as parameters in the function, we can also pass addresses as an argument in functions.

When we pass pointers in the function that means we pass the address of the variable instead of the value.

Let us understand it through a syntax.

As you can see that the func() function takes a reference of the number variable that is the address of the number. And & is used to access the address.


Example: Passing Pointer to Function by reference

Output: After execution of the above code, you will get the following result.

In the above program, we use * in the swapFunc() function with passing reference to swap the number because we are dealing with the address so it is necessary to put * while using those variables.


Return Pointer from Functions

As we know, in C++ that array can be returned from the function, similarly, a pointer can also be returned from a function. For this, we need to declare the returning function as the pointer in the following way.

Also, note that it is considered bad practice to return the address of a local variable from a function, so define the local variable as a static variable.

Now let us see an example

Output:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …