Here are a few questions to help you clarify your understanding of basics of writing your own functions.
What are the three 'parts' of a function?
prototype call definition
What should a function's comments describe?
what it does what it requires before doing its job (input) what it produces (output)
Where should a function's comments be placed?
before both function heads: the prototype and the definition
What does void mean as a function argument list?
no inputs are requested
What does void mean as a function return type?
no value will be returned
How many return statements should a function have?
one (and only one)
Should a function with a void return type have a return statement?
YES!
What difference does it make to place the keyword const in front of a (formal) function argument's type?
It means we won't change the value of that argument during the execution of the function.
Which of the following would be legal calls for this function:
void print_num(double num);
print_num(10.0);YES
print_num(x); // x is of type doubleYES
print_num(10.0 + 6.4);YES
print_num(x * 10.0); // x is of type doubleYES
print_num(10);YES
print_num(10 + x); // x is of type doubleYES
print_num(x * 10); // x is of type longYES
print_num(10 + c); // c is a constant of type doubleYES
In general, what kinds of actual arguments can be passed to this print_num() function?
Any expression which has a double-compatible result type (any numeric type except long double as well as, disturbingly enough, char or bool.
Show both a prototype and a definition for a function to print a friendly greeting to the user.
Prototype: void welcome(void); Definition: void welcome(void) { cout << "Welcome to our program!\n"; return; }
Code both a prototype and a function definition which will add 5 to its single argument and return the result.
Prototype: long add_five(short number); Definition: long add_five(short number) { return number + 5; } (You could also have the argument and return type set to double, if you prefer.)
Write a function to add its two arguments and return their sum. Don't forget the prototype!
Prototype: long add_em(short number1, short number2); Definition: long add_em(short number1, short number2) { return number1 + number2; } (You could also have all the arguments and return type set to double, if you prefer.)
Show a function (prototype and definition) which reads the user's [first] initial and a trailing period; returning the [single] initial to the caller (of the function).
Prototype: char read_init(void); Definition: char read_init(void) { char init, period; cin >> init >> period; return toupper(init); }
Functions can accept any [number of] arguments of any type. Likewise, their return value (if used) can be of any type.
TRUE✗ | FALSE✓ | A function with a void argument list can produce nothing. | ||
---|---|---|---|---|
TRUE✗ | FALSE✓ | A function with a void return value can take no inputs. |
What is meant by the term 'function input'?
Function input means values provided to the function by its caller at the time of the call. These are supplied via the arguments list (between the parentheses).
How does 'function input' differ from 'user input' or 'program input'?
User input and program input are both about the program interacting with the user (typically via the console). These have nothing to do with the input to a function (which is information moving from one part of the program to another).
What is meant by the term 'function output'?
Function output means the answer or product given back to the caller after the function is done executing. This is typically supplied via the return mechanism.
How does 'function output' differ from 'user output' or 'program output'?
User output and program output are both about the program displaying information to the user (typically via the console). These have nothing to do with the output of a function (which is information moving from one part of the program to another).
For the four functions you wrote above, fill in the following table.
Function | (number & type of) Function Inputs |
(number & type of) Function Outputs |
---|---|---|
print greeting | 0 void | 0 void |
add 5 | 1 short | 1 long |
add arguments | 2 shorts | 1 long |
read initial | 0 void | 1 char |
Did you have to look at the definition to determine these things or just at the prototypes? Why?
Just the prototype. It lists all the arguments and their types as well as the return type.