Lecture120 Global variables

Lecture120 Global variables - Quadratic Equation Program...

This preview shows pages 1–5. Sign up to view the full content.

Lecture 1 -- 1 Computer Science I - Martin Hardwick Quadratic Equation Program Remember the quadratic equation program that from several weeks ago. it takes the coefficients A, B, and C of a quadratic equation  Ax 2  +Bx +C = 0  and computes the real roots using the quadratic equation We want to convert this program into a function that can easily be used in any program that needs to compute the roots of a quadratic equation. the function computes and returns the roots given the  coefficients A, B, and C Problem: there may be zero, one, or two roots and a function can return at most one value using the return mechanism. B 2 – 4AC 2A x = -B ±

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

View Full Document
Lecture 1 -- 2 Computer Science I - Martin Hardwick Returning Multiple Values From a Function There are two ways to return multiple values from a function: global variables reference parameters The preferred approach is to use reference parameters. Global Variables – special variables that are available for all functions to use. hence, one function can put a value into a global variable and  another function can retrieve that value from the global variable Reference Parameters – special type of parameter that lets a called function change the value of the corresponding argument variable in the calling function.
Lecture 1 -- 3 Computer Science I - Martin Hardwick Global Variables #include <iostream> using namespace std; // global variables int a,b; void func1 () // called first { a = 10;   // global a b = 2; // global b . . . } void func2() // called last { int b,c; c = a; // global a cout << c; b = 1; // local b . . . } A global variable is a variable declared at the beginning of a program outside all functions. remember, variables  declared inside a function  are local to that function All functions can use a global. unless a function has a  local variable or parameter  with the same name as a  global variable in this case, the global  variable is hidden from the  function Global variables make programs hard to read and understand. therefore, don’t use them

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

View Full Document
Lecture 1 -- 4 Computer Science I - Martin Hardwick Parameter Passing In Functions In all the programs we have seen so far, the arguments in a function call provide data needed by the called function they pass data into the function There are times when a function wants to use its parameters to pass
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/30/2012 for the course CSCI 101 taught by Professor Hardwick during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 17

Lecture120 Global variables - Quadratic Equation Program...

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

View Full Document
Ask a homework question - tutors are online