functionExample.c #include <stdio.h> #include <stdlib.h> #include <string.h> #in
ID: 3750591 • Letter: F
Question
functionExample.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main ()
{
void printHeading ()
{
printf("Add/Sub demo: ");
}
int add (int a, int b)
{
int c;
c = a + b;
return c;
}
int sub (int a, int b)
{
return a - b;
}
int main ()
{
int m, n;
// no parameters
printHeading ();
//with direct values:
printf ("5 + 6 = %d ", add(5,6));
//capturing the return
m = sub (6,5);
printf ("6 - 5 = %d ", m);
//with variables
m = 7;
n = 8;
printf ("%d + %d = %d ", m, n, add (m,n));
//return value into variable
m = m + odd (m, n);
//nested application:
m = odd (m, add(m, sub(n,m)));
printf ("m = %d ", m);
}
}
2.3a - mode the method main () in functionExample.c to the top of the file (but after the include's) so thatit's the first method. What is the compiler error generated?
2.3b - add function prototypes to the top of you functionExample.c file and verify this removes compiler messages when int main is moved to the top.
Explanation / Answer
(a)
The code will look like
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main ()
{
int m, n;
// no parameters
printHeading ();
//with direct values:
printf ("5 + 6 = %d ", add(5,6));
//capturing the return
m = sub (6,5);
printf ("6 - 5 = %d ", m);
//with variables
m = 7;
n = 8;
printf ("%d + %d = %d ", m, n, add (m,n));
//return value into variable
//m = m + odd (m, n);
//nested application:
//m = odd (m, add(m, sub(n,m)));
printf ("m = %d ", m);
return 0;
}
void printHeading ()
{
printf("Add/Sub demo: ");
}
int add (int a, int b)
{
int c;
c = a + b;
return c;
}
int sub (int a, int b)
{
return a - b;
}
The code cause compile time error. It is because, there is no declaration or the prototpe of the function given above the main() function. SO, the compiler function doesn't know the function is available or not. So, in order to remove this error, you need to declare the prototypes of the function above the main function. The prototype consist only the header of the function and not the body.
(b)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// add prototypes of the function
void printHeading ();
int add (int a, int b);
int sub (int a, int b);
int main ()
{
int m, n;
// no parameters
printHeading ();
//with direct values:
printf ("5 + 6 = %d ", add(5,6));
//capturing the return
m = sub (6,5);
printf ("6 - 5 = %d ", m);
//with variables
m = 7;
n = 8;
printf ("%d + %d = %d ", m, n, add (m,n));
//return value into variable
//m = m + odd (m, n);
//nested application:
//m = odd (m, add(m, sub(n,m)));
printf ("m = %d ", m);
return 0;
}
void printHeading ()
{
printf("Add/Sub demo: ");
}
int add (int a, int b)
{
int c;
c = a + b;
return c;
}
int sub (int a, int b)
{
return a - b;
}
now, we have added the prototypes of the function. So, now there is no error.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.