Functions

This is a discussion on Functions within the C Programming forums, part of the General Programming Boards category; Hello guys... i have a doubt over the topic functions where we pass value.. why do we use another variable ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    14

    Functions

    Hello guys... i have a doubt over the topic functions where we pass value..

    why do we use another variable while calling a function .. i mean if we have declared variable x in main() and we use the variable y in function but the role is same..

    i dont understand this concept?

    i think , you understood wat am trying to say?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Because functions can be called from more than one place in a program. So the variable with which it is called could be different in each case. Inside the function it's consistent.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep, I think I get your meaning...

    You don't *have* to use a different variable name. However, the X inside the function is going to be a copy of the X you declared in main... once inside the function changing the value of X will not change the value of the X in main.

    Most often when naming functions and parameters I try to be descriptive of what's going on and will choose variable names that describe the data ...
    Code:
    int InitNewFile( char* Filename, int InitialSize);
    Don't forget some functions are called from dozens of places in a program so the use of descriptive names becomes very helpful.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    But the values does change when we pass the parameters by reference, i mean by calling the address of the variable..isnt it?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vijayshankar View Post
    But the values does change when we pass the parameters by reference, i mean by calling the address of the variable..isnt it?
    Yes, if you pass in a pointer (which is still call by value) you can "dereference" the pointer to operate on the data from inside the function. It works like this...

    Code:
    void foo(int x)
       { x =  x - 2; }    // x in main is NOT changed
    
    void bar(int *x)
       { *x = *x - 2; }  // x in main is changed
    
    int main (void)
    int x;
    
    foo(x)
    bar(&x);
    }
    Last edited by CommonTater; 02-18-2011 at 07:01 AM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    If we pass an address and if the function makes a change to the value pointed to by that address. But the name of the address outside the function is still independent of the names used within the function. The function is generic enough to operate on whatever parameter name is passed. The function could have been written by someone else... with no knowledge of what variable names will be passed to it. That's the point of 'formal' vs. 'actual' parameters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21