Question

In: Computer Science

*Answer in C program* Given a program as shown below: #include <stdio.h> void function1(void); void function2...

*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. Briefly explain how we call a function. Use the above program as example.
  2. Without going into detail, what does a function call do?
  3. What happens after a function has finished executing?
  4. What types of functions available?
  5. In this program, have the programm passed any information from main to function1?
  6. In this program, have the program passed any information from main to function2?
  7. Where does function2 store the values?
  8. In general, what do we need to define and make use of a programmer-defined function?
  9. What is a function prototype?
  10. What is a function definition?
  11. What is a function call?
  12. Must the number, order and type of parameters in the arguement list of a function call and its defination match?
  13. Can we say more about the relationship between main and function1?
  14. Is it necessary that main be the first function defined?
  15. Can a function prototype be written in a function body rather than outside the body of all functions?
  16. Is there any relationship between the variable m in main and the variable m in function2?
  17. What would have happened if we had called function2 from main with two integers or two double in the parameter list instead of an integer and a double?
  18. We used the printf function for this program. Why is there no function prototype for it?

Solutions

Expert Solution

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


Related Solutions

Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
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...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
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...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void quicksort(int number[25],int first,int last){ int i, j, pivot, temp; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(number[i]<=number[pivot]&&i<last) i++; while(number[j]>number[pivot]) j--; if(i<j){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } temp=number[pivot]; number[pivot]=number[j]; number[j]=temp; quicksort(number,first,j-1); quicksort(number,j+1,last); } } int main() { int i, count, number[25]; printf("Enter some elements (Maximum 25): "); scanf("%d",&count); printf("Enter %d elements: ", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1); printf("The Sorted Order is: "); for(i=0;i<count;i++) printf(" %d",number[i]); return...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b...
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b 6 #define t 6 double bmical(double w, double h){ double o; double bmi; o = pow(h,2); bmi = w/o; return bmi; } double maxheartrate(int num1, int age){ double mhr; mhr = num1 - age; return mhr; } double heartratereserve(double mhr, double rhr){ double hrr; hrr = mhr - rhr; return hrr; } double minrange(int hrr, int rhr){ double mirt; mirt = (hrr * 0.7)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT