C functions exchange information by meaning of parameters and arguments. The term parameter refers to any declaration within the parentheses following the function name in a function declaration or definition. The term argument refers to any expression within the parentheses of a function call.
- Function
In C programming, the modularity can be achieved through the functions. The program written in C language are highly dependent on functions. a function is a self-contained or a sub-program of one or more statements that performs a special task when called.
Function Declaration
Function_name (arguments/parameters list)Argument declaration; {
local variable declaration;
statement 1;
statement n;
return (value);}
Example :
#include <stdio.h>
int main () {
int x=1, y=2, z;
z = add (x,y)
printf ("z=%d",z);
}
int add(a,b); {
int a, b;
return (a + b);
- Arguments
Functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.
Arguments to functions specified with prototypes are converted to the parameter types specified in the prototype, except that arguments corresponding to an ellipsis (...) are converted as if no prototype were in scope. (In this case, the rules in the following bullet apply.)
Example :
int main (argc, argv)
argc refers to the number of command line arguments passed in, which includes the actual name of the program, as invoked by the user. argv contains the actual arguments, starting with index 1. Index 0 is the program name.
So, if you ran your program like this:
./ hello world
Then:
- argc would be 3.
- argv[0] would be "./program".
- argv[1] would be "hello".
- argv[2] would be "world".
0 komentar:
Posting Komentar