Connecting...

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++
For more information, please see full course syllabus of Introduction to C++
Introduction to C++ Functions, Part 2
Lecture Description
In this lesson, our instructor Alvin Sylvain gives an introduction to part two of functions. He starts by discussing the parameters by value and reference, as well as default parameters. He then moves on to return value, overloading and modularization.
Bookmark & Share
Embed
Share this knowledge with your friends!
Copy & Paste this embed code into your website’s HTML
Please ensure that your website editor is in text mode when you paste the code.(In Wordpress, the mode button is on the top right corner.)
×
Since this lesson is not free, only the preview will appear on your website.
- - Allow users to view the embedded video in full-size.
Next Lecture
Previous Lecture
2 answers
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
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
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;