L09 - scope, recursion, function overloading

L09 - scope, recursion, function overloading - We've been...

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

View Full Document Right Arrow Icon
We've been talking about scope: • Block scope • Global scope • Static scope • We've looked at a number of examples in isolation – what scope is applicable in various circumstances • Today – Scope – a comprehensive example – New topic: recursion and iteration
Background image of page 1

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

View Full DocumentRight Arrow Icon
Example 5: Comprehensive scope example #include "stdafx.h" using namespace System; // function prototypes void funcA (void); void funcB (void); void funcC (void); //global variable int x = 1;
Background image of page 2
int main() { int x = 5; //local to main Console::WriteLine(L"local x in outer scope of main is {0}", x.ToString() ); { // start new block int x = 7; Console::WriteLine(L"local x in inner scope of main is {0}", x.ToString() ); } Console::WriteLine(L"local x in outer scope of main is {0} \n", x.ToString() ); funcA(); // funcA has an automatic local x funcB(); // funcB has a static local x funcC(); // funcC uses the global variable x funcA(); // funcA reinitializes its automatic local x funcB(); // the static local x retains its prior value funcC(); // the global variable x also retains its prior value Console::WriteLine(L"local x in main is {0}", x.ToString() ); return 0; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
void funcA(void) { int x = 25; // reinitialized on each call to funcA Console::WriteLine(L"local x in funcA is {0} after entering funcA", x.ToString() ); x++; Console::WriteLine(L"local x in funcA is {0} before exiting funcA \n", x.ToString() ); } void funcB(void) { static int x = 50; // static intialization only on first call to funcB Console::WriteLine(L"local x in funcB is {0} after entering funcB", x.ToString() ); x++; Console::WriteLine(L"local x in funcB is {0} before exiting funcB \n", x.ToString() ); } void funcC(void) { Console::WriteLine(L"global is {0} after entering funcC", x.ToString() ); x *=10; Console::WriteLine(L"global is {0} before exiting funcC \n", x.ToString() ); }
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Iteration and Recursion • We’ve seen applications in which a function is called by another function – since main is a function, any function that is called by statements in main is a function called by another function • Most modern programming languages allow functions to call themselves – and this is called recursion – Let’s look at the basic idea in the context of a simple example (the text uses vertical numbers (p. 765) and integral powers of a double (p. 779) as examples –you can read as well as I can, so we’ll do a different example!)
Background image of page 6
Computing n factorial, where n > 0 remember that n! = n*(n-1)*(n-2)*…*1 int nfact (int n) {i n t i ; double result= n; for (i = n-1; i >0; i--) result = result * i; return result; } • this solution is iterative or we could compute n! like this (again as long as n > 0): int rnfact (int n) { int x = n; if (n == 1) return x; else return (x * rnfact (n-1)); }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course EE 361 taught by Professor Conry during the Spring '08 term at Clarkson University .

Page1 / 26

L09 - scope, recursion, function overloading - We've been...

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

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