• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

return or memory address

hi,

is it more efficient to assign value to memory address by passing pointer to the function or by using return...
for instance...

void myfunc(int * a)
*a=4;

or
int myfunc(int a)
return a=4;

thanks

0
zizi21
Asked:
zizi21
  • 2
  • 2
  • 2
  • +1
1 Solution
 
figrocCommented:
for your example, it's exactly the same.
0
 
zizi21Author Commented:
yup...but for others it might be different..which would be more better...

for instance, if i call return 1000 times would be more time consuming than using pointer....since we are not returning anything....or it does not make any diffrene...
0
 
sunnycoderCommented:
It really does not matter and it depends on the implementation ... C does not dictate how pointers get assigned and how values get returned ... Each implementation is free to optimize them as they please.
In any case the difference, if any is insignificant.

>if i call return 1000 times would be more time consuming than using pointer
You would return after the first return so effectively 999 times it wont be executed.

The choice between returning and using an output parameter is generally dictated by need and coding conventions.
Many softwares use return value to indicate success/failure and output parameters for communicating values.
At times when your function can change the value it is being passed and this change needs to be reflected in the caller, you have to use pointers.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Infinity08Commented:
It does matter whether you return or pass it as parameter.

For example :

        void fun(Big *bp) {
            bp->data = someValue;
            bp->data2 = someOtherValue;
        }

        Big someBigObject;
        fun(&someBigObject);

allows the function to work on the original Big object. While :

        Big fun() {
            Big b;
            b.data = someValue;
            b.data2 = someOtherValue;
            return b;
        }

        Big someBigObject;
        someBigObject = fun();

would potentially copy the data twice (once to place it in the local functionBig object, and once to return it). The compiler MIGHT optimize this, but there's no guarantee that it can do that.

The first version will most likely be faster.
0
 
zizi21Author Commented:
thanks
0
 
sunnycoderCommented:
The underling assumption of comparison is that we are passing comparable data size ... Returning a pointer to someBigObject intead of a someBigObject makes more sense for comparison ...
0
 
Infinity08Commented:
@sunnycoder, following the example from the original question only strengthens my argument. Take a look at :

        void fun(Big *bp) {
            bp->data = someValue;
            bp->data2 = someOtherValue;
        }

        Big someBigObject;
        fun(&someBigObject);

versus :

        Big fun(Big b) {
            b.data = someValue;
            b.data2 = someOtherValue;
            return b;
        }

        Big someBigObject;
        someBigObject = fun(someBigObject);

The second is even worse when it comes to copies.


Remember that the question was about :

>> void myfunc(int * a)
>> *a=4;

versus :

>> int myfunc(int a)
>> return a=4;

There are no pointers in the second version.
0

Featured Post

Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now