Search code examples
cprototype

What types of prototypes exist in C?


Well, i read somewhere (i dont remember where) that are like types of prototypes in C. Ones used to sending parameters and others used for data return. What are those named and what are those used for ?


Solution

  • "...Ones used to sending parameters and others used for data return...."

    Using common terms, you may be referring to function parameter types, in particular as they have to do with input parameter and output parameter.

    In actuality, C does not distinguish or define input and output function parameters per se. By definition C has only pass by value function parameters. However, the type of value that is passed can be one of two distinct categories: either the value represented by the object itself, or the value of the address of the object. If it is the value of the object itself that is passed, then the called function is not able to modify that value. If however the value of the address of the object is passed, then the function is able to modify the value of the object residing at that address, and upon function return, the updated value of the object is accessible via the return function parameter.

    So, to simplify wording for this illustration, and in the context of this answer the following terms are used:

    • input - function parameter that passes value of object.
    • output - function parameter that passes the value of the address of the object and provides access to updated object value upon return.

    To illustrate, with respect to input and output the following prototype has both. The first two arguments are input, and the third is output.:

    void func1(int val1, int val2, int *sum)
    {
        *sum = val1 + val2;
    }
    
    called like this: 
    
    int a = 100;
    int b = 300;
    int sum = 0;
    
    int return = func1(a, b, &sum);//value of sum == 400 upon return