In: Computer Science
*Answer in C program*
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.
1) To call a function write the following
function_name( parameters );
wherever you want to call the fujncion in the program. Make sure the function is already declared and defined
In the above program, inside void main(void) block
you can see function1(); statement which is calling function1() wherever it is defined
Also just after this statement you can see function2(m, y); which is again a function call where m and y are the parameters that are being passed (by value) .... Note : we have two ways of passing -> by reference ->by value
2) Function call simply sends the control to the funtion definition
In the above program the control is being send from the main() to function1() definition after it encounters the calling statement
3) After the function has finished executing, it returns the control back to the calling statement (either with a value if return value is specified or without any value if return type is void )
In the above program both the functions doesnt hjave any return type so just after executing the respective functions the control comes back to main() function.
4) Basically we have two types of function
-> Standard Library Functions (sqrt(), sort(), etc. )
-> User defined functions
Now we have four types of user defined functions
a) Function with no arguments and no return value.
b) Function with no arguments and with return value.
c) Function with arguments and no return value.
d) Function with arguments and with return value.
5) No, nothing is passed from main() to function since it is a function of no return type and no arguments
6) Yes m and y variables are passed to the function2() as arguments with m as integer and y as double respectively since it is a function with arguments and no return type
7) Actually C language doesnt telly you explicitly what you want to store where, so it basically stores the parameters being passed to it in the stack, but this is not it , everything that can be stored in stack can also be stored in registers.
8)In general to define and make use of programmer - defined functions or user defined functions we need to do the following
-> Function declaration : we need to declare a function with its prototype ie ., (to tell the compiler that this is the we are going to use with given datatypes)
return_type function_name( datatype_of_argument1, datatype_of_argument2, ....);
in the above program function is defined above the main() function.
-> Function Definition : we need to define every function for operation or execution we need to perform
return_type function_name( datatype_of_argument1 variable1, datatype_of_argument2 variable2, ....)
{ .................(statement 1).................
..................(statement2).................
.
.
.
}
in the above program function is defined below the main() function
-> Function call : function can be called wherever we want our function to be executed it can be called in this way
function_name( argument1, argument2, .... );
in the above program function1() and function2() are called inside the main() function
9) Function prototype : it is a way of declaring a function ie. (telling the compiler we created this function and going to use it in this program) . Prototype specify function name and the signature (ie. the type of arguments and return type)
return_type function_name( datatype_of_argument1, datatype_of_argument2, ....);
in the above program
void function1(void);
void function2 (int, double x); these are function declaration using the prototype shown above
10) Function Definition : It is the block which contains the statements to be executed by the function
we need to define every function for operation or execution we need to perform
return_type function_name( datatype_of_argument1 variable1, datatype_of_argument2 variable2, ....)
{ .................(statement 1).................
..................(statement2).................
.
.
.
}
in the above program function is defined below the main() function
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);
}
this is the function definition of function1() and function2()
11) Function call : function can be called wherever we want our function to be executed it can be called in this way
function_name( argument1, argument2, .... );
in the above program function1() and function2() are called inside the main() function
Function call sends the control to the function definition along with the arguments if any
12)Yes the order, number and type of parameters should match otherwise it will lead to error but this can happen in case function overloading where a function can have different prototype;
13) main() function is same as the function1() or any other normal function with return type and but the only difference is main() is called by the operation system not by the user inside the program like we do with normal functions
14) no it is not necessary to define the main() function first. One can define the function at the time of declaration only ie before main()
15) Yes function prototype can be written in function body but the issue is this fuction you are creating will be local to that function only ie. one can not call this new function from outside the function is is declared in
16) the variable m insidemain() is not same as variable inside function2(). While passing the argument m to the function2() only the value of m is being passed and not the variable m, the both variables inside the function2() and main have the same value only but not the same address. As the function is called function creates its own variable with the same value and hence any changes the variable in function2() will not affect the value in main()
17) While passing arguments (in case of double and integer it doesnt matter what data type it is it will implicitly convert it ) for eg it the type is float and you pass int a =90 it will be automatically converted into 90.00 and will be passed the the function2(). Now imagine if the argument type is int and you are passing double a = 12.24 it wil be implicitly converted to 12 . The only disadvantage is you might not get the answer you are expecting as there is a loss of data
18) As we have discussed in 4) we have two types of functions
-> Standard Library Functions (sqrt(), sort(), etc. )
-> User defined functions
so printf is a standard library function only and for standard library functions we dont need function declaration or prototype instead we include header files .
In this case we are using "stdio.h" by including this we are telling the compiler that we will be using this header file's built-in function