Question

In: Computer Science

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 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. In general, what do we need to define and make use of a programmer-defined function?
  2. What is a function prototype?
  3. What is a function definition?
  4. What is a function call?
  5. Must the number, order and type of parameters in the arguement list of a function call and its defination match?
  6. Can we say more about the relationship between main and function1?
  7. Is it necessary that main be the first function defined?
  8. Can a function prototype be written in a function body rather than outside the body of all functions?
  9. Is there any relationship between the variable m in main and the variable m in function2?
  10. 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?
  11. We used the printf function for this program. Why is there no function prototype for it?

answer all the question please

Solutions

Expert Solution

--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 '}'.

  • void function1(void)
  • {
  • printf("function1 is a void function that does not receive\n\\r values from main.\n\n");
  • }

--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.


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...
*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;...
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; }
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
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> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT