270 likes | 293 Views
ㅎㅎ. 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
E N D
ㅎㅎ 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 • Occurs in XML as well
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
“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
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).
Sample function int add2ints(int a, int b) { return(a+b); } • parameters • Return type • Function name • Function body
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); }
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);
[Practice2] Square Loot [예제 5]
[Practice2] Square Loot [예제 6]
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)
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
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.
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.)