7 Replies - 1691 Views - Last Post: 23 April 2011 - 12:59 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

C passing arguments

Posted 22 April 2011 - 03:41 AM

So im on the awesome journey of teaching myself C programming language. And i ave a question regarding C's passing arguments. As we know C is passing by value - making the copy of the arguemnts so tht original varable contents are not altered. My question is: in what situations would i want to pass my arguments by value? The only common example i can think of is:


int main()
{
   x = 10;
   printMe(x);
   return 0;
}

void printMe(int a)
{
   printf("%d",a);
}




Since my thinking is that most of the time we want to alter the contents of our variables so why would we use assing by value and not by reference.
Thanks a lot for any situations where we would use it and please note that i am a noob at C.

Is This A Good Question/Topic? 0
  • +

Replies To: C passing arguments

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: C passing arguments

Posted 22 April 2011 - 04:32 AM

View Postdarek9576, on 22 April 2011 - 04:41 AM, said:

Since my thinking is that most of the time we want to alter the contents of our variables so why would we use assing by value and not by reference.


There are many examples, you don't always need to pass a reference, it's actually more common not to, I would say.

As for an example, how about a function that adds two numbers are returns the result?

int add(int a, int B)/> {
    return a+b;
}

Was This Post Helpful? 1
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: C passing arguments

Posted 22 April 2011 - 04:51 AM

Good point :).
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C passing arguments

Posted 23 April 2011 - 10:43 AM

I disagree; more often then not you need to minimize copying and such. Passing a const reference/pointer is when you know you don't want to modify the parameter passed. Non const when you do.

/C++
//passing by value results in a copy for a and b
//better:
int add(const int& a, const int& b ) {
    return a+b;
}
//C 
int add(const int* a, const int* b ) {
    return *a+*b;
}




edit: wrong language, woof.
Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: C passing arguments

Posted 23 April 2011 - 10:49 AM

The equivalent in C:

//passing by value results in a copy for a and b
//better:
int add(const int * const a, const int * const b );

as in
#include <stdio.h>

int add(const int * const a, const int * const b);

int main(void)
{
    int a = 9;
    int b = 33;

    printf("The answer: %d\n", add(&a, &b));

    return 0;
}

int add(const int * const a, const int * const b)
{
    return *a + *b;
}


Was This Post Helpful? 1
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C passing arguments

Posted 23 April 2011 - 10:50 AM

Whoops, this was C. :oops:

Thanks Jack.
Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: C passing arguments

Posted 23 April 2011 - 12:34 PM

Yes, i also wanted to mention the const keyword. When i was reading about this topic in the book, at the end of the chapter the author mentioned that very often you want to save memory in C therefore copying is not the way to go and he introduced const with pointers as you mentioned above :).
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: C passing arguments

Posted 23 April 2011 - 12:59 PM

It should be noted there is a reason you don't see:
int add(const int * const a, const int * const b );


Because a pointer is generally the same size as an int. Thus, it saves absolutely nothing over int add(int a, int b ); in terms of space. Worse, it makes calling the function much more awkward.

It's nice to be able to just say:
printf("The answer: %d\n", add(9, 33));



Also, a copy of the value means it's your copy to mess with. Knowing that you can play with the copy passed to you without effecting anything it often useful in C.

This post has been edited by baavgai: 23 April 2011 - 01:00 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1