< Previous | Contents | Next >

Functions: Mad Lib


Every program youve seen so far has consisted of one functionmain().

However, once your programs reach a certain size or level of complexity, it becomes hard to work with them like this. Fortunately, there are ways to break up big programs into smaller, bite-sized chunks of code. In this chapter, youll learn about one waycreating new functions. Specifically, youll learn to:

n Write new functions

n Accept values into your new functions through parameters

n Return information from your new functions through return values

n Work with global variables and constants

n Overload functions

n Inline functions


 

Creating FunctionsIntroducing the Instructions ProgramDeclaring FunctionsDefining FunctionsCalling FunctionsUnderstanding AbstractionUsing Parameters and Return ValuesIntroducing the Yes or No ProgramReturning a ValueAccepting Values into ParametersUnderstanding EncapsulationUnderstanding Software ReuseWorking with ScopesIntroducing the Scoping ProgramWorking with Separate ScopesWorking with Nested ScopesUsing Global VariablesIntroducing the Global Reach ProgramDeclaring Global VariablesAccessing Global VariablesHiding Global VariablesAltering Global VariablesMinimizing the Use of Global VariablesUsing Global ConstantsUsing Default ArgumentsIntroducing the Give Me a Number ProgramSpecifying Default ArgumentsAssigning Default Arguments to ParametersOverriding Default ArgumentsOverloading FunctionsIntroducing the Triple ProgramCreating Overloaded FunctionsCalling Overloaded FunctionsInlining FunctionsIntroducing the Taking Damage ProgramSpecifying Functions for InliningCalling Inlined FunctionsIntroducing the Mad Lib GameSetting Up the ProgramThe main() FunctionThe askText() FunctionThe askNumber() FunctionThe tellStory() FunctionSummaryQuestions and AnswersDiscussion QuestionsExercises