Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Intermediate C++
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

More With Functions

  • Modify your “Auto” program (of course!)
    • Set it up so you can run it from the command line and give the first parameter as the name of the input file
    • Have it test the number of parameters, and complain if there isn’t at least one
    • If there is more than one, the second one will be the name of the output file, otherwise, either use some default name or print with “cout”.
    • Extra: use command line switches, including“-h” to print out “help” for the user and “-v” for the version. Use “-i” to indicate the name of the input file, and “-o” to indicate the name of the output file.
      • It should work in any order, e.g.: myProgram -i inputFile -o outputFile
      • should work just as well as: myProgram -o outputFile -i inputFile
      • The program should complain if no input file was provided.
    • More Extra: program it to work whether or not the user includes a space between the “-i” and the parameter.
    • Still More Extra: use a “-f” for a parameter to indicate the name of a file which contains the names of each input and output file, so you can automatically run a series of tests with one command.
    • Still Even MORE Extra: Use the “map” from a previous lesson so that you can use a “properties file” such as, “INPUT_FILE= {name of input file}”, etc.
  • Write a command-line program that accepts an integer input parameter then uses a recursive algorithm to calculate the factorial.
    • Don’t forget to validate the input as appropriate.
    • A “stringstream” object may be useful for converting the input character string array to an integer.
  • Big Extra: Write a recursive search algorithm – similar to looking up a word in the dictionary – this is rather sophisticated, you might want to keep this one for future use (just in case you can’t use a “map” for whatever reason).
    • Create a class to contain an “Index” and a “Value”, where “Index” is an integer, and “Value” can be any type you’d like.
    • Provide an overloaded “<” operator that returns true if the left class Value is less than the right class Value.
    • Read in a list of values from a file. For each value read, set the “Value” of a new class object, and then store the class object in a vector.
    • Use the vector’s “sort()” method to put them into numerical order (it will use your overloaded “<” operator)
    • Iterate through the vector, starting at zero, set each “Index” to one more than the previous. After this point, the contents are in ascending order with their own unique index number, and each “Index” happens to match the vector index.
    • Prompt the user for a value to search for.
    • Call a recursive function with the value being searched, the vector to search through, and the minimum and maximum index to search through. If it returns false, print “No such value.”
    • The function should look at the point halfway between the minimum and maximum index. If the Value content at that point is the value being searched for, print out its index and return true.
    • If the Value content at that point is greater, return the recursive function call with a minimum index equal to the calling index, and a maximum index equal to the halfway point just tested.
    • If less, then the halfway point is the recursive minimum, and the maximum matches the maximum.
    • If the minimum and maximum indexes are already equal to each other, (or the max is less than the min), and the Value doesn’t match, it means the search value isn’t in the vector. Return false.
  • More Big Extra: All this recursive function does is print the value when it finds it, which isn’t much use in a general case.
    • Add a parameter to the function, a pointer to a function to be called when the value is found.
    • Create a function that receives the class object (“Index” and “Value” created above) and prints out “The value ‘{search value}’ was found at ‘{Index}’”.
    • Pass that function into the recursive function, which of course then recursively passes it into each recursive call.
    • Should the value be found, instead of printing the value, pass the found object to the dereferenced function pointer so that function can do the work.

More With Functions

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Overview 0:18
  • Parameters for 'Main' 2:18
    • First Function
    • The Way Seen Most Often
  • Some Typical Parameters 6:05
    • Help
    • Version
    • User
    • Password / Port
    • File To Be Operated On
  • Code Example 8:25
  • Some Things of Note 12:17
    • First Item in 'argv' is the Program File
    • How Each Argument is Given to the Program is OS Dependent
    • A Return of '0' Usually Means Success
  • More Things of Note 15:11
    • It's Customary to Have Both Stand-Alone Arguments (Options) and Arguments with Additional Parameters
    • Examples
  • Inline Functions 17:37
    • 'inline' Tells the Compiler to Insert the Function Body Explicitly Into the Code
    • Function Modularity Without Function Overhead
    • Example
  • Recursive Functions 20:22
    • Definition of Recursive: A Function or Algorithm That Uses Itself to Solve a Problem
    • Common Mistakes
  • Classic Example: Factorial 21:27
  • Recursive Factorial Code 23:16
    • Compare Iterative Version
  • Pointers to Functions 25:55
    • Technique Used Often in GUI Programming
    • The Package Calls the Caller's Function by Dereferencing the Pointer
    • Example
  • Pointers to Functions as Parameters 27:45
    • Syntax
    • Calling Parameter Function Within Function
    • Example
  • Multi-Dimensional Array Parameters 31:07
    • Function Receives a Pointer to Array But Can Not Know Size Ingo
    • Example
  • Ways to Handle; 1-Dim Array 33:53
    • Good use for an Inline Function
  • Ways to Handle: Internal Data 35:38
    • Example 1
    • Example 2
  • Ways to Handle: Objects 39:38
    • Example
    • Usage Examples
  • No Help Here! 42:28