C++ Workshop - Functions, Parameters, & Scope (Ch. 5)

Started by
45 comments, last by me_minus 14 years, 7 months ago
Heya All,

Sorry if it seems as though I've been MIA for the last few days. I've been at the hospital, and am only online long enough to pass along the news and then I must return to the hospital.

My wife gave birth to our baby boy, Connor Riley Walsh, on June 28th at 11:02am. At birth he was 20" long and 6lbs 9ounces.

Unfortunately, he was born with his cord around his neck and was having problems breathing and feeding. As a result, he's been in the NICU for the last 2 days and will remain there for another day or two. As soon as I return with my wife and baby from the hospital I will get the answers posted for the quiz.

Sorry again for being offline!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
Hmm - I suppose that's a good enough excuse!!!

Congratulations. I hope that everything goes well for them both from now on. Your colleagues have been fielding the questions and comments but don't let that lull you into the belief that your detailed explanations aren't also required.

I look forward to your return, although you may have bleary eyes as your sleep's likely to be disturbed for quite some time.
Many congratulations to you & your wife for your new arrival! Overwhelming isn't it? I hope everything works out fine for him. In case this is your first don't worry; it gets easier after 6 weeks, then 3 months, then 6 months, even if it doesn't seem so at the time. If we don't see you here for a long time your expertise will be sorely missed but it would be completely understandable, and thank you so far for your considerable help; finally I have a chance to learn something I've wanted to learn for the past decade.

simesf
While I'm offline, can one of the tutors or moderators that has the book post questions from the chapter in the format of my previous quizzes?

Thanks!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:Original post by Zahlman

twoaterisn: Like FireNet said. Except, don't include .cpp files! You *can* include any kind of file, but it won't necessarily produce something that compiles ;)



Hehe, that will depend on what sort of interface (IDE,text editor,whatever) you are using. If you are using an IDE then just include the .cpp file in the project, and declare the functions before you use them, like in a header file, which you can include in your source, and the .cpp file where you are defining the function.


If you are not using an IDE, and just an editor, you will have to include both. The header first then the source. In that case things will be processed by most compilers sequentially, like they were all in one file. You will have to worry about not including a file more than once, or provide defs to exclude already included files ;).

I would recommend you use IDEs, for it may be too much for the begginer to mess with command line compilers, I've and it was quite fun, but needs a lot of time :P.




______________________________________________________________________________________________________
[AirBash.com]
Congratulations Jeromy!
Greetings All!

Sorry I was unable to post a quiz last week. I was attending my son in the NICU as many of you have already read. I was hoping another of the tutors would be able to post a quiz in my stead, but alas, they were all occupied as well. However, it's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapter 5. Each chapter builds upon the knowledge obtained in previous chapters, so it can be dangerous and confusing to advance to later chapters without a complete understanding of the material already covered.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.

Chapter 5 Quiz

1. Where do global functions exist? Where do member functions exist?
2. What is a function?
3. When a function identifier is encountered, where does execution jump to? Where does it return to after the function terminates?
4. How do you declare a function’s return value type?
5. What is a parameter list?
6. Can you use a function before it has been declared? Can you use it before it has been defined?
7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?
8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
10. Must a function prototype includes the names of variables in the parameter list?
11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
12. What do we call variables created within the scope of a block?
13. What happens to those variables when the function returns to a higher scope?
14. [Extra Credit] Are these variables created on the stack, or the heap?
15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
16. What do we call the default passing method which implies the creation of temporary variables?
17. What is a global variable? Where can it be accessed?
18. What happens to global variables when there is a local variable with the same name?
19. Why are global variables dangerous?
20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
21. What can be passed as input to a function? What is the only requirement?
22. How do you return a value from within a function?
23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
25. What is function overloading? What benefit does it provide?
26. What is recursion? When is it useful?

Chapter 5 Exercises

There are no exercises this week as I am currently preparing the first workshop project instead.

Cheers and Good luck!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Hopefully it isn't too late to post about this chapter but I'm getting a compiling error with listing 5.7. I copied it from the book and I think it's 100% exact. I have a fever right now and I'm on meds so I could be hallucinating.

// Listing 5.7 - demonstrates use of default parameter values

#include <iostream>

int AreaCube(int length, int width = 25, int height = 1);

int main()
{
int length = 100;
int width = 50;
int height = 2;
int area;

area = AreaCube (length, width, height);
std::cout << "First area equals: " << area << "\n";

area = AreaCube (length, width);
std::cout << "Second time area equals: " << area << "\n";

area = AreaCube (length);
std::cout << "Third time area equals: " << area << "\n";
return 0;
}

AreaCube (int length, int width, int height)
{
return (length * width * height);
}


I get this error:

------ Build started: Project: listing5_7, Configuration: Debug Win32 ------
Compiling...
listing5_7.cpp
c:\documents and settings\crimz\desktop\htp\listing5_7\listing5_7\listing5_7.cpp(26) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Build log was saved at "file://c:\Documents and Settings\crimz\Desktop\htp\listing5_7\listing5_7\Debug\BuildLog.htm"
listing5_7 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Sorry don't know how to do that fancy source code thing.

You forgot the "int" in front of the implementation of AreaCube.

This is hinted at by the compiler in the line number ( the 26 after the source file name ) and the error message "missing type specifier".

The ( rather cryptic ) statement concerning default-ints is a relic from C++'s lineage from C, in which any function that didnt have a type was assumed to return an int...
Quote:Original post by rip-off
You forgot the "int" in front of the implementation of AreaCube.

This is hinted at by the compiler in the line number ( the 26 after the source file name ) and the error message "missing type specifier".

The ( rather cryptic ) statement concerning default-ints is a relic from C++'s lineage from C, in which any function that didnt have a type was assumed to return an int...



Oh thanks. The book just has it written "AreaCube(int length" etc... without the int.

I've noticed this book has several typos. Eh.

This topic is closed to new replies.

Advertisement