Chapter 6: Function (Continued)

There are three ways to declare a function

1. Define the function before it is called by any other function.

What the program will do

Compare two integers in the variables a and b.
If a is less than b, the function will return -1.
If a is equal to b, the function will return 0.
If a is greater than b, the function will return 1.

Solution

Create a file named myfile.c, write the following code:

int numcmp(int a, int b)
{
if(a<b)
{
return -1;
}
else if(a==b)
{
return 0;
}
else
{
return 1;
}
}

int main()

{

    int x=10, y=20, z;

    z=numcmp(x,y);

    printf(“%d”,z);

    return 0;

}

Compile the above code and run.

2. Declare the prototype of the function.

The prototype is generally used for a function defined after the main() in C language. All the components of a prototype and the function must be identical except that the parameter variables do not exist in prototype.

I would like to learn more!