In: Computer Science
Given a program as shown below:
#include <stdio.h>
void function1(void);
void function2 (int, double x);
void main (void)
{
int m;
double y;
m=15;
y=308.24;
printf ("The value of m in main is m=%d\n\n",m);
function1();
function2(m,y);
printf ("The value of m is main still m = %d\n",m);
}
void function1(void)
{
printf("function1 is a void function that does not receive\n\\r values from main.\n\n");
}
void function2(int n, double x)
{
int k,m;
double z;
k=2*n+2;
m=5*n+37;
z=4.0*x-58.4;
printf ("function2 is a void function that does receive\n\\r values from main.The values received from main are:\n\\r\t n=%d \n\r\t x=%lf\n\n", n,x);
printf ("function2 creates three new variable, k, m and z\n\\rThese variable have the values:\n\\r\t 1=%d \n\r\t m=%d \n\r\t z=%lf \n\n",k,m,z);
}
1) Function prototype (also called function declaration) has 3 parts:
But not the functionality of the function. Thus, in above program function prototype is
void function1(void);
void function2 (int, double x);
2) Function Definition includes Function Prototype and task which that function should perform. Thus everything declared inside "void main(...) {.....}, void function1(void) { ..... } , void function2 (int, double x) {.....} is function definition of their respective function.
3) When a function needs to be executed it is called using its name and suitable arguments and this is knoen as Function Call.
E.g. function1(); , function2(m,y);
Depending on return type of function, a same type variable can be used to store return value from the called function.
Extra: A main() function is only function which is called by compiler while execution of program starts.
4) Yes, the number, order and type of parameters in arguments list of a function call must exactly match as of its definition and its prototype or declaration. This is because compiler doesn't know which argument is for which parameter, it's something we on our own must decide. One can declare some predefined values in function declaration itself to prevent passing default argument again and again.