Introduction to C++ Functions, Part 2

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.

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 WhiteFri Oct 31, 2014 7:42 AM

Post by Timothy Whiteon 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 SylvainThu Jan 10, 2013 2:56 PM

Post by Karpis Sanosyanon 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 SylvainSun Oct 28, 2012 7:44 PM

Post by David McNeillon 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 functionint 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 Abrahamon 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

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.