C Tutorial – More on Functions
In this C programming language tutorial we will talk some more about functions. We will take a look at command-line parameters and function prototypes.
Command-line parameters
In some cases you want to give a parameter at the start of a program.
For example:
# myprogram -i
The program myprogram will start and something extra will be done because of the command-line parameter -i (What it will do is up to you, this is just an example).
Now let’s make a program that will print the words that are typed behind the program at execution time. (Compile the program with the name myprogram).
Here is the example:
#include<stdio.h>
int main(int argc, char *argv[])
{
int x;
printf("%d\n",argc);
for (x=0; x < argc; x++)
printf("%s\n",argv[x]);
return 0;
}
After compiling the program “myprogram” start it as follows:
# myprogram aa bb cc dd
In this code, the main program accepts two parameters, argc and argv. The argv parameter is an array of pointers to a string that contains the parameters entered when the program was invoked at the UNIX command line. (Pointers will be explained in a later tutorial, for now it is enough to know that it points to an address in memory where the parameters are stored). The argc integer contains a count of the number of parameters, including the program-name, which is the first one in the array of pointers argv. In this example the count is five (program-name plus four flags, aa, bb, cc and dd).
First the program will print the number of parameters given when the program was invoked (stored in argc). This number will be used in a “for loop”. In this case it will print four times.
The second printf statement will print the parameters given when the program was invoked, one by one. Try it! Command-line parameters can be used for many things.
Function prototypes
A function prototype declares the function name, its parameters, and its return type to the rest of the program. This is done before a function is declared. (In most cases at the beginning of a program).
To understand why function prototypes are useful, try the following program:
#include<stdio.h>
void main()
{
printf("%d\n",Add(3)); /* <- There is the error ! */
}
int Add(int a, int b)
{
return a + b;
}
Note: just one parameter when the function is called. (Not a typo).
The example above will be compiled on many of the compilers. They just give some warning, because the function Add() needs two parameters as input, not one. The result of this program cannot be foreseen. It works because many C compilers do not check for parameters matching either in type or count. The result is that you will spend a lot of time debugging programs, because you made a mistake of passing on to many or too few parameters to a function.
To solve this problem you can make use of function prototypes. If you use prototypes, C checks the types and count of the parameter list.
Try the next example:
#include<stdio.h>
int Add (int,int); /* function prototype for Add */
void main()
{
printf("%d\n",add(3)); /* <- There is the error ! */
}
int Add(int a, int b)
{
return a + b;
}
When you try to compile this program, the compiler will flag an error on the printf statement.
So in the future use function prototypes for every function you declare. It will save you a lot of time!
Hey there! where can i learn “call by reference” kinda programs on this tutorial?
also can you show an example/tutorial “called by value”
some how understandable