Q1: Is add_salad supposed to be another function that we call from main?


Q2: Can you give me any helpful hints on how to go about solving this because I've spent hours trying all sorts of methods but I have failed so far.


Q3: How can I put a call-by-reference parameter into:
double add_salad (char deliver, int n_person)?


Q4: What is the proper way of writing an inline function?


Q5: What's wrong with my inline function?: inline void add_tip (price) {return price *= 1.15;}


Q6: I know that void shouldn't have a value to return...but if it does't...what does it do here? inline void add_tip (price)
{return price *= 1.15;}


Q7: On the Functions handout, page 2, would the inline function call in main() look like? "float avg(n1, n2)"


Q8: When using pointers and references, would it look like the following:
(*var) :in declaration
(&var) :in fn call
(*var) :in definition


Q9: What should the inline function add_tip() look like using reference types?


Q1: Is add_salad supposed to be another function that we call from main?

A1: Yes. Look at the PA (Programming Assignments) handout, page 7 Step b)
It lists the function declarations as:
double add_salad(void);
double add_salad(char deliver, int n_person = 10);
Look at Step e). It talks about add_salad() in great detail.
Look at HINTS at the bottom of the page. Step 4) and then Step 5).


Q2: Can you give me any helpful hints on how to go about solving this because I've spent hours trying all sorts of methods but I have failed so far.

A2: How about your notes from lecture? Did you write down the information
from the blackboard specifically on PA#5?
This was about 20 minutes of explanation, pseudocode,and hints. You also want to see the Proctors for specific questions. Also office hours. Maybe look at another student's notes for PA#5.


Q3: How can I put a call-by-reference parameter into:
double add_salad (char deliver, int n_person)?

A3: PA#5 asks to use only reference types (call by reference parameter)
only with "inline void add_tip(double & rprice);"
Therefore NOT with "double add_salad (char deliver, int n_person = 10);
You will have to pass parameters (call by value). With any of these function calls:
salad_price = add_salad ( ans ); // default 10 persons
salad_price = add_salad ( ans, num_person);
Note:
salad_price = add_salad ( ); // calls void add_salad(void); for 20 persons


Q4: What is the proper way of writing an inline function?

A4: Function declaration from PA handout, page 7, Step b)
inline void add_tip ( double & rprice);
To make it inline, just place keyword "inline" in front:
a) If you have both function declaration and function definition
OR
b) If you have only a function definition placed before its call.


Q5: What's wrong with my inline function?:
inline void add_tip (price) {return price *= 1.15;}

A5: Remember, 1st line of function definition (header) is like the function declaration.
inline void add_tip ( double & rprice)
{ ... }


Q6: I know that void shouldn't have a value to return...but if it does't...what does it do here?
inline void add_tip (price) {return price *= 1.15;}

A6: Right, void does not return a value, therefore the compiler complains because you told it NOT to save any space for a return value. So it didn't. AND now you are trying to return a value!

Remember you are using a reference type parameter. It is an alias for your argument you passed to the function. Therefore, you passed in the price from main(). add_tip() is accessing the original value and returns NO value. Do all the same function body AND do *NOT* use the keyword "return".


Q7: On the Functions handout, page 2, would the inline function call
in main() look like? "float avg(n1, n2)"
A7: INLINE FUNCTIONS
1) First of all avg() is a function
2) 2nd, this is the function definition for avg().
3) 3rd, it is inline
> inline float avg(int n1, int n2) {return ((n1 + n2)/2.0;)}
4) You call this FROM main(), just like in the previous example on
that handout, page 2, top of the page
Note: The function declaration is REQUIRED because the function
definition is *AFTER* the call ( main() comes 1st )
The function call does *NOT* have a data type!
==========================================================================
float avg ( int n1, int n2 ); // function declaration
main()
{ int i1 =1, i2 = 2, av;
av = avg(i1, i2); ... // function call
}
float avg ( int n1, int n2 ) // function definition
{
float average; average = (n1 + n2)/2.0; return (average);
}
__________________________________________________________________________
This would be the same as:
NO function declaration needed because, the function definition is BEFORE its call (main() comes 2nd)
==========================================================================
float avg ( int n1, int n2 ) // function definition
{ float average; average = (n1 + n2)/2.0; return (average); }
main()
{ int i1 =1, i2 = 2, av;
av = avg(i1, i2); ... // function call
}
__________________________________________________________________________
We can rewrite these to be even more simple, omitting local variables:
==========================================================================
float avg ( int n1, int n2 ) // function definition
{ return ( (n1 + n2)/2.0; ) }
main()
{ int i1 =1, i2 = 2, av;
av = avg(i1, i2); ... // function call
}
__________________________________________________________________________
We can modify this to be inline function for faster runtime by
inserting the keyword inline in front of the function definition.
==========================================================================
inline float avg (int n1, int n2){return((n1 + n2)/2.0;)} // fn definition
main()
{ int i1 =1, i2 = 2, av;
av = avg(i1, i2); ... // function call
}
__________________________________________________________________________
If we have BOTH function declaration and function definition, keyword
inline must be inserted before BOTH.
==========================================================================
inline float avg (int n1, int n2); // fn. declaration
main()
{ int i1 =1, i2 = 2, av;
av = avg(i1, i2); ... // function call
}
inline float avg (int n1, int n2){return((n1 + n2)/2.0;)} // fn definition

Q8: When using pointers and references, would it look like the following:
(*var) :in declaration
(&var) :in fn call
(*var) :in definition

A8: POINTERS
1) Look at PA handout, page 6
2) Used ONLY in calc_price();
3) Function declaration, Step b)
void calc_price( double *p_price, int n_person);
4) Function call, Step d)
calc_price( &my_price, num_person);
5) Function definition
void calc_price( double *p_price, int n_person)
{
*p_price += 3.98; ... // Through pointer, change my_price in main()
}


Q9: What should the inline function add_tip() look like using reference types?

A9: REFERENCE TYPES
1) Used ONLY in add_tip();
2) inline used ONLY in add_tip();
3a) IF ONLY a function definition
inline void add_tip(double &rprice){rprice=...;} // change my_price in main()
3b) IF a function definition AND function declaration
inline void add_tip(double &rprice); // function declaration
inline void add_tip(double &rprice){rprice=...;} // function definition


TOP


Home || Course Description || Course Outline || Labhours || FAQ


Webmaster: Nilofer Motiwala

Last updated July 13 1997