In this, we are going to see how to pass a pointer as a Argument/parameter to a function in C++ Programming Language.
The Code given below can be used for TURBO C++ Compilers: -
//C++ program for passing pointer as argument
#include <iostream.h>
#include <conio.h>
float value(float *x)
{
*x = 0;
return *x;
}
void main()
{
clrscr();
float x;
cout << "Enter a number:-\n";
cin >> x;
cout << "Before calling the function:- " << x << endl;
value(&x);
cout << "After calling the function:- " << x;
getch();
}
//...........Coded by YASH ALAPURIA
The Code given below can be used for gcc/g++ Compilers: -
//C++ program for passing pointer as argument
#include <iostream>
#include <stdlib.h>
using namespace std;
float value(float *x)
{
*x = 0;
return *x;
}
int main()
{
system("cls");
float x;
cout << "Enter a number:-\n";
cin >> x;
cout << "Before calling the function:- " << x << endl;
value(&x);
cout << "After calling the function:- " << x;
return 0;
}
//...........Coded by YASH ALAPURIA
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP