Enter your Sign on user name and password.

Forgot password?
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 Introduction to C++
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Lecture Comments (11)

2 answers

Last reply by: Alvin Sylvain
Thu Jun 25, 2015 6:55 PM

Post by Alex Moon on June 24, 2015

Hello, I don't understand what is being asked here.

2. Write a function that takes a float parameter, and uses it as the divisor into two, then subtracts two. That is, do the following arithmetic: (2.0 / parameter + 2.0)

Return the result in the parameter, but have a Boolean return value of “false” if the arithmetic can not be performed because the parameter is too small (we want to avoid getting overflow errors or divide-by-zero errors), or “true” otherwise. You will have to test the parameter before performing the operation.

Thus, if the function returns “true”, the parameter has been changed to its new value, and the caller may use it. Otherwise, it remains unchanged, and the return value tells the caller the parameter can not be used.

In "Return the result in the parameter, but have a Boolean return value of “false” if the arithmetic can not be performed ... or “true” otherwise"
is it telling me to create a function that can either return a boolean AND a float result?
Should the program print the value passed by reference in parameter? Thank you

2 answers

Last reply by: Timothy White
Fri Oct 31, 2014 7:42 AM

Post by Timothy White on October 30, 2014

Not necessarily related to this lecture, but is it possible to use C++ in a webpage? This is the first language I have learned that is not geared toward web development, so I wanted to know if there is a way to do so.

1 answer

Last reply by: Alvin Sylvain
Thu Jan 10, 2013 2:56 PM

Post by Karpis Sanosyan on January 10, 2013

Hello I have love the lessons and they really help, but i have a quick question. Its not as much as related to the lessons but to educator.com it's self. Could you please possible make an android and iPhone app for this site. Because I find that I don't always have my computer when I'm on the go and want to watch the lectures.

1 answer

Last reply by: Alvin Sylvain
Sun Oct 28, 2012 7:44 PM

Post by David McNeill on September 30, 2012

Alvin I hope you could help me move on to the next section in my course.
when doing homework for this section "functions part 2" i got rather confused on question 2. After struggling to understand the problem i came up with a solution but i don't know if it is the correct solution. Please help me move on so i know i understand what i have learned up until this point. here is my solution please let me know if I have understood your question:

// Name : Function_Return_Boolean.cpp
// Author : David K McNeill
// Version : 1.0
// Copyright : Your copyright notice
// Description : checks a parameter is not to small before calculating and assigning the result to a variable.

#include <iostream>
using namespace std;

// Function that checks if a parameter is not to small to divide.
// If parameter is not to small this function will perform the following arithmetic:
// 2.0 / parameter -2; it will then pass out the result to a parameter and return the
// value true to the caller.
// if the parameter is to small then it will return false to the caller.
bool divide_Add (float para, float & result, bool & a) {
//result = (2.0 / para -2);
if (para == 0) {
a = false;
else {
result = (2.0 / para -2);
a = true;
return a;

//global variables.
float para = 10; // parameter to be checked if its to small.
bool test = true; // variable that is assigned the function boolean return value.
bool a = false; // variable that determines what value the function returns.
float actual = 0; // variable we want to assign the function arithmetic to if all is Ok.
float result = 0; // variable that we assign the value of function arithmetic.

// main function
int main() {

test = divide_Add (para, result, a); // this is the call to the function
if (test == true) { // if function returns true we will assign the function arithmetic to are variable.
actual = result; // and print out "yippe it worked" as well as the value of 'actual', 'result' and 'para'.
cout << "Yippe it worked."<<endl;
cout << actual <<endl;
cout << result <<endl;
cout << para <<endl;
else {
cout << "number to small to use." <<endl; // if function returns false the this warning will be printed
} // and no value assigned to are variable.
return 0;

0 answers

Post by Alex Abraham on August 19, 2012

In the first example, the code is more broken than you think :p

The first time, it takes 7, adds 7, and gets 14.

The second time, it takes 14, adds 14, and gets 28.

The third time, it takes 28, adds 28, and gets 56.

One way to fix this error is to say something like: number = second; before the for statement and change the command in the for statement to number += second;

Also at the end it should say return number;

Functions, Part 2

  • A function may have “call by value” parameters, where the caller provides input arguments for the function to work on. The function is free to change the values of these parameters without affecting the caller. Where possible, this is the preferred method to increase isolation and maintainability.
  • A function may have “call by reference” parameters, where the caller expects the function to return new values for the input arguments.
  • A function may have “call by address” parameters, where the caller passes pointers as parameters.
  • A function may have default parameters that provide a default value for any argument not provided by the caller.
  • Each function usually has exactly one return value, or the caller’s “answer”, that will be used by the caller.
  • Functions may be overloaded which allows the same function name to be used with different prototypes.
  • Functions are crucial to creating modular code, avoiding code duplication, allowing more readable and reusable code. Readable code is easier to maintain. Reusable code saves time and effort. Modules should be insulated from their containing systems to decrease external dependencies, and increase reusability
  • Here are links to more information about C++ functions: http://www.cplusplus.com/doc/tutorial/functions/http://www.cplusplus.com/doc/tutorial/functions2/

Functions, Part 2

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:09
  • Parameters by Value 0:47
    • Example
  • Parameters by Reference 2:55
    • Example
  • Parameters by Reference Example 4:20
  • Default Parameters 6:36
    • Acceptable Example
    • Unacceptable Example
  • Default Parameters Example 7:42
  • Return Value 8:41
    • Special Type: Void
  • Overloading 11:07
    • Name Accordingly & Carefully
    • Example
  • Overloading Example 12:31
  • Modularization 13:41
    • Smaller Modules
    • Isolated Modules
  • Cut Into More Pieces 16:31