tut5lecturenotes[1] - An Introduction to Programming with...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
An Introduction to Programming with C++, Third Edition Tutorial 5 Tutorial 5 Void Functions Lecture Notes Overview In tutorial 4 the concepts involved with a value-returning function were covered. In this tutorial the concept and implementation of a void function will be covered. In addition, the concept of passing information to a function by reference will be introduced. Contrast will also be made with value-returning vs. void functions, as well as passing by value vs. passing by reference. Objectives After completing the tutorial, the student will be able to: Create and invoke a function that does not return a value Pass information, by reference, to a function Pass a String variable, by value and by reference, to a function Instructor Notes More About Functions Remember from the previous tutorial that a function is a block of code that performs a task. Every C++ program contains at least one function called main(), and most programs contain many more functions. Programmers use functions for two reasons: functions allow the programmer to avoid duplicating code in different parts of the program, and they allow large and complex programs to be broken down into small and manageable tasks. Functions are categorized as either value-returning functions or void functions. Value-returning functions return a value to the statement that called the function, whereas void functions do not return a value. Therefore, a void function will not contain a return statement at the end of its function body, as with a value-returning function. Creating Void Functions When defining a void function you must code the function header and the function body. The function header is the first statement in the function definition. It specifies the name of the function, and any optional parameters the function may have. The following is the syntax for a void function header: void functionName([parameterList]); . Note that the rules for naming functions are the same as the rules for naming identifiers. 5 - 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
An Introduction to Programming with C++, Third Edition Tutorial 5 Note the difference between the function header of a value-returning function vs. the function header of a void function. The only difference between the two is that the header in a void function begins with the keyword void, where the header in a value-returning function begins with the return data type of the function. After coding the function header the function body must be coded. The function body will contain the instructions necessary for the function to accomplish its task. The function body will begin with an opening brace ( { ) and end with a closing brace ( } ). Unlike a value-returning function, a void function does not contain a return statement. This is due to the fact that a void function does not return a value.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

tut5lecturenotes[1] - An Introduction to Programming with...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online