1 / 27

Namespace Function Recursive function Call by value

ㅎㅎ. Fourth step for Learning C++ Programming. Namespace Function Recursive function Call by value. C++ Namespaces. What is this strange ‘ std:: ’ in std::cout ? - Concept of Namespaces - Why does it exist? We want to use the same identifier in several different contexts

Télécharger la présentation

Namespace Function Recursive function Call by value

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ㅎㅎ Fourth step for Learning C++ Programming • Namespace • Function • Recursive function • Call by value

  2. C++ Namespaces • What is this strange ‘std::’ in std::cout ? • - Concept of Namespaces - Why does it exist? We want to use the same identifier in several different contexts • Occurs in XML as well

  3. Namespaces • Example for Defintion: namespace myNamespace { int a, b; } • Example for use of namespace:myNamespace::a myNamespace::b • a and b occur in the namespace myNamespace

  4. “using” a namespace • Example:#include <iostream> using namespace std;int main () { cout << 5.0 << “\n”; } • from here on we use the namespace std • std:: is not necessary now

  5. C++ Functions • In other languages called subroutines or procedures. • C++ functions all have a type. • Sometimes we don’t need to have a function return anything – in this case the function can have type void. • You have decide on what the function will look like: • Return type • Name • Types of parameters (number of parameters) • You have to write the body (the actual code).

  6. Sample function int add2ints(int a, int b) { return(a+b); } • parameters • Return type • Function name • Function body

  7. C++ Programming Sample Function int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; // just to make a point firstnum = 0; secondnum = 0; return(sum); }

  8. [Practice1] Function

  9. [Practice1] Function

  10. C++ Programming double sqrt( double ) • When callingsqrt, we have to give it a double. • The sqrt function returns a double. • We have to give it a double. x = sqrt(y); x = sqrt(100);

  11. [Practice2] Square Loot [예제 5]

  12. [Practice2] Square Loot [예제 6]

  13. [Practice 3] Math Operator Rules

  14. C++ Programming Scope of variables • Remember: The scope of a variable is the portion of a program where the variable has meaning (where it exists). • A variables scope starts with its definition, it is never known before its definition (declaration)! • A global variable has global scope. (until end of file) • A local variable’s scope is restricted to the function that declares the variable. (until end of function) • A block variable’s scope is restricted to the block in which the variable is defined. (until end of block)

  15. [Practice4] Recursive Chicken-Egg

  16. [Practice4] Recursive Chicken-Egg

  17. C++ Programming Recursive Function • Computing Factorial Step 9: factorial(4) returns 24 (4*6) Main function:factorial (4) factorial(4) is called in the main factorial (4) = 4*factorial(3) Step 8: factorial(3) returns 6 (3*2) Step 1: factorial(4) calls factorial(3) factorial (3) = 3*factorial(2) Step 7: factorial(2) returns 2 (2*1) Step 2: factorial(3) calls factorial(2) factorial (2) = 2*factorial(1) Step 6: factorial(1) returns 1 (1*1) Step 3: factorial(2) calls factorial(1) factorial (1) = 1*factorial(0) Step 5: factorial(0) returns 1 Step 4: factorial(1) calls factorial(0) factorial (0) = 1 C++ Programming L3.17

  18. [Practice 5] factorial

  19. [Practice 5] factorial

  20. Storage Class • static – created only once, even if it is a local variable. • extern – global variable declared elsewhere. • auto – created each time the block in which they exist is entered. • register – same as auto, but tells the compiler to make as fast as possible.

  21. Storage Class – static • Declaring a local variable as static means it will remember it’s last value (it’s not destroyed and recreated each time it’s scope is entered). • Local variables are auto by default. (created each time the block in which they exist is entered.)

  22. [Practice 6] Storage Class - static

  23. [Practice 6] Storage Class - static

  24. [Exercise 1] Count Down

  25. [Exercise 2] Fibonacci Numbers

  26. [Exercise 2] Fibonacci Numbers

More Related