It also allows you to reuse code, in the same program or in several programs. Taking a working subroutine and applying it to as many places as possible is an effective way to deal with larger programming tasks.
Understanding functions is essential to make you a better programmer, hence a more desirable engineer.
function.cpp and into a function ... menu
(...) in a new file menu.cpp. The part that you
should take out is writing the menu to the screen and getting the
menu letter from the user
Function menu should return the letter selected
by the user to the main function. Main will still do
the processing appropriate to that choice, (input a material cost,
compute a part, quit, or print an error message.) (25%)
Hint: You will need to declare storage locations for the
letter, say menu_choice both inside main and inside
menu.
Hint: Function menu must return a character to
the main function. What does that mean for the
declaration/prototype of function menu?
Hint: menu will be called in the main function
using an assignment statement.
Hint: Function menu does not receive any
information from the main function. What does this mean for
the declaration/prototype of function menu?
Hint: Unless you can answer the first three hints, do not even try to do any programming. See lesson 9 on functions if you cannot answer them.
main function further as
follows: Take the part of the main program that prompts the user
for a part's steel and brass weights and the cost of these
materials, and that then computes and prints the part's total
material cost. Put all of this into a function part_process.
Add it to the project file list. (25%)
Hint: Function part_process does not need to return
anything to the main program, since it should already print out
the part's material cost itself.
Hint: Function part_process will of course need the
costs of brass and steel. Figure out the right way to get
these from main into part_process (Lessons 10 and 14).
Hint: Do at least two parts and check that you do not have to reenter the costs of steel and brass the second time.
Select the best answer:
Select the best answer:
#include <iostream.h>
void three(void) { cout << "3 "; }
void two(void) { cout << "2 "; three(); }
void one(void) { cout << "1 "; two(); }
void main(void) { cout << "Counting "; one(); }
3 2 1 Counting
Counting 1 2 3
#include <iostream.h>
void Jack(void);
void John(void) { cout << "John "; Jack(); }
void Jane(void) { cout << "Jane "; }
void Jill(void) { cout << "Jill "; John(); }
void Jack(void) { cout << "Jack "; Jane(); }
void main(void) { cout << "Hi "; Jill(); cout << "!";}
John Jack Jane Jill John Jack Jane Hi Jill!
John Jane Jill Jack Hi !
Hi Jack Jill Jane John !
Hi Jill John Jack Jane !
Jack is the last function
before the main function.
Jack is not defined twice!
void hi(void) {
cout << "Hello\n" ;
hi();
}
Hello lines to the screen.
#include <iostream.h>
void set(void) {
int a;
a=1;
}
void main(void) {
int a;
a=0;
set();
cout << a;
}
void between the parentheses when set was called
inside main.
#include <iostream.h>
int a;
void set(void) {
a=1;
}
void main(void) {
a=0;
set();
cout << a;
}
#include <iostream.h>
void out(int houses, int horses) {
cout << houses << " " << horses;
}
void main(void) {
int boats=10, sheep=2;
out(boats, sheep);
}
void before out in main.
int before boats and
sheep in the call to out in main.
houses horses
boats sheep
10 2
#include <iostream.h>
void show(float a) {
cout << a << endl;
}
void main(void) {
float a=1, b=2;
show(b);
}
a, not b
#include <iostream.h>
void out(int a, int b) {
cout << a << " " << b << endl;
}
void main(void) {
int a=1, b=2;
out(b,a);
}
1 2
2 1
#include <iostream.h>
int out(int a, int b) {
cout << a << " " << b << " ";
return a+b;
}
void main(void) {
cout << "(" << out(1,2) << ")" << endl;
}
cout statement.
(1 2)
(3)
(1 2 3)
(1 2 ) 3
(a b ) 3
#include <iostream.h>
int fun(int i) {
if (i==1) return 1;
else return i*fun(i-1);
}
void main(void) {
cout << fun(1) << " " << fun(2) << " " << fun(3) << " "
<< fun(4) << " " << fun(5) << endl;
}
0 0 0 0 0
1 2 3 4 5
1 2 6 8 10
1 2 6 24 120
#include <iostream.h>
int ratio(int a, int b) {
return a/b;
}
void main(void) {
int a=1, b=2;
cout << ratio(a,b) << " " << ratio(b,a);
}
ratio(a,b) ratio(b,a)
a/b a/b
0 0
0.5 0.5
0.5 2
0 2
2 2
#include <iostream.h>
void out(int i) {
cout << i << endl;
}
void main(void) {
out(1.5);
}
h.
#include <iostream.h>
void swap(int a, int b) {
int tmp;
tmp=a;
a=b;
b=tmp;
}
void main(void) {
int a=1, b=2;
cout << a << " " << b << " --- ";
swap(a,b);
cout << a << " " << b << endl;
}
1 2 --- 1 2
1 2 --- 2 1
1 2 --- b tmp
#include <iostream.h>
void swap(int &a, int &b) {
int tmp;
tmp=a;
a=b;
b=tmp;
}
void main(void) {
int a=1, b=2;
cout << a << " " << b << " --- ";
swap(a,b);
cout << a << " " << b << endl;
}
&
before the a and b.
a and b
must be reference variables in main.
a and b
must be aliases in main.
1 2 --- 1 2
1 2 --- 2 1
1 2 --- b tmp
void error_msg(char *msg) function so that,
for example, error_msg("This is a test") or
error_msg("You made a mistake!") will print out these
messages along with a beep and a new line. Use your function
to print out the ``You entered an invalid choice'' message
in the menu.
get_brass_cost and get_steel_cost. These
should take no parameters.
input_positive_float which inputs a
float from the user, but does not accept a negative of zero value.
Use this function to obtain the costs of brass and steel
from the user.
input_nonnegative_float to
input the weights of steel and brass of the part.
error_msg function that can either
print just a character string, or a character string followed by a
floating point number. Use it for the error messages when the
user inputs an invalid number.