answersLogoWhite

0


Best Answer

You would use a pointer as a parameter when you want to pass the parameter by reference, meaning your function has a reference to the object being passed in, rather than simply a copy of that object. This means that changes to that object made in the function will persist once that function has completed.

The below example should print:

x = 3

x = 4

Note: I just coded this from memory so you will probably have to tweak it slightly to get it to compile

int main()

{

int x = 3;

passByVal(3);

cout << "x = " + x;

passByRef(3);

cout << "x = " + x;

return 0;

}

void passByVal(int a)

{

a++; // a is a copy of x, a different object

}

void passByRef(int &b)

{

b++; // b is a reference to the same object that x points to

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Like any other value/variable -- nothing special.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you use pointers as a function arguments?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you use arguments in main function?

Yes.


Is Use of pointers in function saves the memory space?

No. But avoiding unnecessary duplication of data does.


What is meant by arguments in c?

Arguments appear in functions and in function calls. Arguments passed to a function are known as actual arguments. The arguments used by the function are known as the formal arguments. In C, all arguments are passed by value, such that the formal argument is a copy of the actual argument.


Call by value in c programming?

Its way of passing arguments to a function.In this method the value of actual arguments(the arguments in the function call)remains unchanged. changes that we make are effected only to the formal arguments.eg.#include void modify(int,int);// function prototype. this is given to make the compiler aware that there is a function modify. if we didnt do this a error will be shown. otherwise the function body should be written before the function callmain(){int a,b;printf("enter two numbers");scanf("%d %d",&a,&b);modify(a,b);// function call the parameters a and b are actual arguments.printf(" a and b in the main %d and %d",a,b);}void modify(int a,int b)// function header the parameters no1 and no2 are called as formal arguments.{a=a*3;b=b*3;printf(" a and b in function %d and %d",a,b}output:enter two numbers 32a and b in function 9 and 6a and b in the main 3 and 2.this is a program to multiply 3 to the numbers given by the user . here we use function and the arguments are passed by call by value method.here the value of the formal arguments are altered withinthe function but no change happens to the actual arguments . the values in the main does change even after the function call.


Is it possible for a function to have no arguments?

Of course.

Related questions

How can you use pointers as function arguments?

Like any other value/variable -- nothing special.


Why you use pointers?

Because you can produce fast and efficient code. Function arguments are passed "by value", and so you can't change the original value of the argument, but if you use pointers, you can.


Why use pointer?

Because you can produce fast and efficient code. Function arguments are passed "by value", and so you can't change the original value of the argument, but if you use pointers, you can.


Can you use arguments in main function?

Yes.


What are the two parts required to use a function in a program?

function name and arguments


How do you write a function with a variable number of input elements in PHP?

When defining your function, do not put any arguments in the definition (e.g. function myFuntion()). Inside the function, you can use func_num_args() and func_get_arg($number) to get the function's arguments.


Is Use of pointers in function saves the memory space?

No. But avoiding unnecessary duplication of data does.


What does Excel use to separate multiple arguments in a function?

Commas are used to separate arguments in Excel functions.


What are pointers in c programming language?

Pointers are variables that hold the address to a memory location. It makes copying/assignment very efficient, since it eliminates the need for copying entire memory blocks...only the address is copied. This is useful for example for function arguments.


What are the formal arguments?

Formal arguments are the named arguments defined by the function. Actual arguments are those arguments that were passed to the function by the caller.


The values that you use with a function are called?

values used with a function are called


When does the function arguments box appear in Excel?

It shows you what needs to be put into a function for it to work. You can type the arguments directly into the boxes that are shown for the particular function you are using. It is particularly handy for more complex functions that you are not used to working with.