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);
}
Questions: Please use program given as example in explaination.
answer all the question please
--1.) The programmer defined function defines a specific task in a block of code(known as function) so that the code can be re-used any number of time. Their is no need to write bunch of codes whenever a task (whose function is already defined) needs to be done, simply calling the function and the same bunch of code will be re-used. For example, if there is a need to print content of printf() function which is inside function1, we can simply call function1.
--2.) Function prototype aims at mentioning return type of data that functions will return, number of arguments that will be passed to function, datatypes of each arguments passed and the order in which each arguments. Two prototype in above program are -
1.)1.) void function1(void); - This function will return nothing(void) and it will take no arguments
2.)2.) void function2 (int, double x); - This function will return nothing(void) and it will take two arguments, first as int and second as double respectively.
--3.) The function definition is the actual body part(or content ) of the function. The definition(body) is defined inside curly braces '{' and '}'.
--4.) Function call is referred to as invoking the function to perform the specified task. For example, inside the main() function in above code, user called function1 from main() by stating "function1();". Function is called by,
functionName(argument1 if any, arguement2 if any...)
--5.) Yes, it should match, otherwise compiler will be confused about order or datatype and it will provide error.
--6.) The relationship between main and function1 will always be of caller and callee. main() function is calling function1 so that function1() will execute whatever present inside its body(definition).
--7.) No, it is not necessary the main() function body will be defined first. Any function can have its definition at top of main() function. Function's definition can exist in any order. Writing function1 definition above main() will not change resultant in any way.
--8.) No. Prototype of function should be written at the top of code and not in between any function.
--10.) No, there is no retation between variable m. Local variable(variable inside functions) are subject to be restricted inside theie respective functions. So treat variable 'm' inside main() as seperate entity and 'm' in function2 as different.
--11.) In case of both the arguments passed are int, there would not have been any problem, as int(being smaller in size than double) can fit into double using auto-implicitly. But when both the arguments are passed as double, 'int n'(function2) could not able to take double in its area as int is not supposed to have floating point.
--12.) printf function's defintion is defined in <stdio.h> header file which have been declared at the start of mentioned program(as prototype), so that printf() function functionality can be achieved in this program. The import is a keyword which appends an alreeady existing 'C' file into the current file.