1. What would be wrong with not writing return statement in a value returing fun
ID: 3552780 • Letter: 1
Question
1. What would be wrong with not writing return statement in a value returing function? Can you have a return statement in a void function? Does the return statement in the following function cause syntax error?
void p(double x, double y)
{
cout << x << " " << y << endl;
return x+y;
}
2. Write a function headers (not the bodies) for the following functions;
a. Return a sales commission, given the sales amount and the commission rate.
b.Display the calendar for a month, given the month and year.
c.Return a square root of number.
d.Test Whether a number is even, and return true if it is.
e.Display a message a specified number of times.
f.Return the monthly payment, given the loan amount, number of years, and annual interest rate.
g.Return the corresponding uppercase letter, given a lowercase letter.
Explanation / Answer
1>
C99 and C++ standards don't require functions to return a value. The return with no value in a value-returning function will be defined (to 0) only in the main function.
#include <stdio.h>
int stringSize()
{
}
int main()
{
char cstring[5];
printf( "the last char is: %c ", cstring[stringSize()-1] );
return 0;
}
NOT AN ERROR, BUT WE GENERALLY RETURN FROM NON VOID FUNCTIONS
However, it is always a practice to return value from non void functions.
Yes, we can have a return statement in a void function
void func(int a)
{
int b = 2;
b = a + 2;
return;
}
Yes, we can have a return statement in a void function
void p(double x, double y)
{
cout << x << " " << y << endl;
return x+y;
}
Yes, we encounter a syntax error
compilation info
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.