#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12

    functions and arguments in C programming


    Hi there,

    I am so confused. I have learnt the basic of C programming, however, my basic is too basic that I don't see myself doing anything.

    I am hoping that someone can give me a good explaination of how functions and arguments work.

    All I know is writing my big chunck of codes in my main() function.
    I dont know how to declare the different types of functions (what arguments to include in them, in what format) on the top of my main file.

    What I mean is, if I have an array in my main, int array[10], then I have a function that need to call it, says, my_function( <what to include in the parameters>); and there are various kind of things such as pointer to the array, etc...how do I get my functions right?

    I have read the book by Kernighan & Ritchie, but it's too complicated for me to understand.

    If someone can give me a hand or refer me to some good references it would be great.

    Cheers,
    Geekoid
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Gloucester, Glos
    Posts
    9
    Rep Power
    0
    Hi Geekoid
    You need to look at the problem from the angle of what do you want your programme to do?, and then break it down into smaller pieces these will form your function calls. In the case of an array the name of the array is a pointer to the array start, you can pass this name into the functions parameter list


    func(int a[100])
    {
    int i;
    for(int i =0;i<100 i++)
    a[i]= i;

    }
    main()
    {
    int a[100];

    func(a);

    }

    the best way to learn is via having a go at a problem and using the help files etc. There is also a lot of info on the net.
    good Luck
    Dave.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I also read K&R at first, but that was their first edition which contained syntax that's no longer used, especially in how functions were written. I haven't looked at their second edition (stamped "ANSI C" over the cover), so I don't know if they changed over to the current standard or not.

    Do you have a background in other programming languages that use functions or procedures? If so, then you should already have the basic idea of what to use functions for. In case you don't have that basic idea, I'll briefly describe what functions are used for. If there's something more specific that you don't understand, then ask about it.

    1. Use functions to handle operations that are done more than a few times. For example, if you need to convert between degrees and radians, then you would want to write a function to perform that conversion rather than have to rewrite that formula every single time you use it.

    2. Use functions to carve the overall program up into smaller, more manageable and more comprehensible chunks. This is also called the "divide and conquer" approach and is a part of structured programming.

    Other languages differentiate between functions (which return a value) and procedures (which don't). In C, there are only functions, but the ones declared as void don't return a value.

    The syntax for a function header is:
    return-type function-name(parameter-list)

    The return-type is any valid data type, including the special type void (which is kind of like the zero in our numbering system). If you leave this out of the header, the compiler will assume that the return type is int, but it should also warn you (but usually won't -- it assumes that you're a C programmer so you should know what you're doing).

    The function-name is any valid identifier. C/UNIX folk tradition calls for using the names foo and foobar in examples.

    The parameter-list is a list of parameters separated by commas. The syntax for a list of two parameters is:
    data-type param1, data-type param2
    If there are no parameters, then the keyword void is used or else the parentheses are left empty (I prefer to write void); eg:
    int foo(void)
    double foobar()
    Please note that the parentheses MUST be used even then there are no parameters.

    The syntax of a function definition is:
    function-header
    {
    function-body
    }
    The function-header is as defined above.

    The function-body consists of data definitions and standard C statements, just as you would write them in the main() function. The variables the function-body may use are the parameters in the parameter list, local variables defined within the function-body, and "global" variables that have file scope.

    If the function's return data-type is not void, then the last statement executed in the function must be a return statement. This defines the value that the function returns, so the value in the return statement must match the function's data-type. If the function's data type is void, then the return statement is not required, but it IS required for any and all other data types.

    To call a function, simply use its name in a statement followed by parentheses and parameter list. The number and data types of the parameters must match the parameter list declared in the function header. Parentheses MUST be used, or else you end up telling the compiler to do something completely different.

    Before a function can be called, it must have been declared earlier in the file. This is done by either placing the function before any calls to it to by using a function prototype. A function prototype is simply a function header followed by a semi-colon; eg:
    char foo(int x);
    Function prototypes are placed either near the top of the file or in a header file that the source file includes. The standard library header files that you include in your program contain function prototypes among other declarations.

    The parameters you pass to a function are all "call by value", which means that the parameters are evaluated and their value is what is sent to the function. This means that if the function changes them in any way, those changes do NOT affect the variables used in the function call. Eg, a function to swap two values:
    Code:
    void swap(int a, int b)
    {
        int temp;
    
        temp = a;
        a = b;
        b = temp;
    }
    
    // function call
    swap(x,y);
    would fail to change x and y. To cause the change to a parameter to be applied to the variable used in the function call, you need to pass the address of that variable and the function must be written to expect an address. This is done with pointers; eg:
    Code:
    void swap(int *a, int *b)
    {
        int temp;
    
        temp = *a;
        *a = *b;
        *b = temp;
    }
    
    // function call
    swap(&x,&y);
    In this case, the values of x and y would have been swapped.

    There are two ways to pass an array to a function:
    Code:
    int array[10];
    
    void foo(int a[]);
    void foobar(int *a);
    These two parameter lists are equivalent because an array name is a pointer. Please note that you do not need to include the number of elements in the array, since a[] would be treated as a pointer anyway.

    I hope that helps. If you have more specific questions, ask.
    Last edited by dwise1_aol; June 26th, 2003 at 08:03 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    41
    Rep Power
    12
    Thanks Guys.
    You guys are great.

    I have figured out how to do it.
    And my sorting is running well, :) Hooray...

    Geekoid

IMN logo majestic logo threadwatch logo seochat tools logo