Presentation is loading. Please wait.

Presentation is loading. Please wait.

ㅎㅎ Fourth step for Learning C++ Programming Namespace Function

Similar presentations


Presentation on theme: "ㅎㅎ Fourth step for Learning C++ Programming Namespace Function"— Presentation transcript:

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

2 What is this strange ‘std::’ in std::cout ?
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 09/2013

3 Example for Defintion:
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 09/2013

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 09/2013

5 In other languages called subroutines or procedures.
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). 09/2013

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

7 int add2nums( int firstnum, int secondnum ) { int sum;
Sample Function int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; // just to make a point firstnum = 0; secondnum = 0; return(sum); } Available via the course home page in code/functions/add2nums.cpp C++ Programming 09/2013

8 [Practice 1] Function

9 [Practice 1] Function

10 When calling sqrt, we have to give it a double.
double sqrt( double ) When calling sqrt, 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); C++ Programming 09/2013

11 [Practice 2] Square Loot
[예제 5]

12 [Practice 2] Square Loot
[예제 6]

13 [Practice 3] Math Operator Rules

14 A global variable has global scope. (until end of file)
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) C++ Programming 09/2013

15 [Practice 4] Recursive Chicken-Egg

16 [Practice 4] Recursive Chicken-Egg

17 Computing Factorial Recursive Function C++ Programming C++ Programming
Main function: factorial (4) Step 9: factorial(4) returns 24 (4*6) 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 C++ Programming L3.17 09/2013

18 [Practice 5] factorial

19 [Practice 5] factorial

20 static – created only once, even if it is a local variable.
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. 09/2014

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.) 09/2014

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

27


Download ppt "ㅎㅎ Fourth step for Learning C++ Programming Namespace Function"

Similar presentations


Ads by Google