C++ Functions: Variable Scope Storage Class Recursion
C++ Functions: Variable Scope Storage Class Recursion
C++ Functions
In other languages called subroutines or procedures. C++ functions all have a type.
Sometimes we dont need to have a function return anything in this case the function can have type void.
C++ Functions
C++ Functions
Sample function
me a n n o i t c Fun
Return type
parameters
Fun ctio n bo dy
C++ Functions
C++ Functions
C++ Functions
x = sqrt(y);
The stuff we give a function is called the argument(s). Y is the argument here. A C++ function cant change the value of an argument! If y was 100 before we call sqrt, it will always be 100 after we call sqrt.
C++ Functions
C++ Functions
Writing a function
You have decide on what the function will look like:
Return type Name Types of parameters (number of parameters)
Function parameters
The parameters are local variables inside the body of the function.
When the function is called they will have the values passed in. The function gets a copy of the values passed in (we will later see how to pass a reference to a variable).
C++ Functions
12
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++ Functions 13
Testing add2nums
int main(void) { int y,a,b; cout << "Enter 2 numbers\n"; cin >> a >> b; y = add2nums(a,b); cout << "a is " << a << endl; cout << "b is " << b << endl; cout << "y is " << y << endl; return(0);
C++ Functions
14
Local variables
Parameters and variables declared inside the definition of a function are local. They only exist inside the function body. Once the function returns, the variables no longer exist!
Thats fine! We dont need them anymore!
C++ Functions
16
Block Variables
You can also declare variables that exist only within the body of a compound statement (a block): { int foo; }
C++ Functions 17
Global variables
You can declare variables outside of any function definition these variables are global variables. Any function can access/change global variables. Example: flag that indicates whether debugging information should be printed.
C++ Functions 18
Scope
The scope of a variable is the portion of a program where the variable has meaning (where it exists). A global variable has global (unlimited) scope. A local variables scope is restricted to the function that declares the variable. A block variables scope is restricted to the block in which the variable is declared.
C++ Functions 19
C++ Functions
20
Block Scope
int main(void) { int y; { int a = y; cout << a << endl; } cout << a << endl; }
C++ Functions 21
t s i x e t n k! s e oc o l d b a e h t r ro ide r E ts ou
Nesting
In C++:
There is no nesting of function definitions.
You dont need to know who calls a function to know the scope of its variables!
C++ Functions
22
Nested Blocks
void foo(void) { for (int j=0;j<10;j++) { int k = j*10; cout << j << , << k << endl; { int m = j+k; m cout << m << , << j << endl; } } }
C++ Functions
23
Storage Class
Each variable has a storage class.
Determines the period during which the variable exists in memory. Some variables are created only once (memory is set aside to hold the variable value)
Global variables are created only once.
C++ Functions
24
Storage Classes
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. static created only once, even if it is a local variable. extern global variable declared elsewhere.
C++ Functions 25
static
example
{ 0;
int countcalls(void) static int count = count++; return(count); } cout << countcalls() cout << countcalls() cout << countcalls()
C++ Functions
28
Function Prototypes
A Function prototype can be used to tell the compiler what a function looks like
So that it can be called even though the compiler has not yet seen the function definition.
A function prototype specifies the function name, return type and parameter types.
C++ Functions 30
Example prototypes
double sqrt( double); int add2nums( int, int); int counter(void);
C++ Functions
31
Using a prototype
int counter(void); int main(void) { cout << counter() << endl; cout << counter() << endl; cout << counter() << endl; } int counter(void) { static int count = 0; count++; return(count); }
C++ Functions 32
C++ Functions
33
Dualing Functions
string chicken( int generation ) { if (generation == 0) return("Chicken!"); else return(egg(generation-1)); } string egg( int generation ) { if (generation == 0) return("Egg!"); else return(chicken(generation-1)); }
C++ Functions 34
Recursion
Functions can call themselves! This is called recursion. Recursion is very useful its often very simple to express a complicated computation recursively.
C++ Functions
36
C++ Functions
38
C++ Functions
39
Recursive Step
Use the recursive call to solve a subproblem.
The parameters must be different (or the recursive call will get us no closer to the solution). You generally need to do something besides just making the recursive call.
C++ Functions
41
C++ Functions
42
C++ Functions
43
Recursion Exercise
Write a function that prints a triangle:
triangle(4); triangle(5);
References
A reference variable is an alternative name for a variable. A shortcut. A reference variable must be initialized to reference another variable. Once the reference is initialized you can treat it just like any other variable.
C++ Functions
46
C++ Functions
47
C++ Functions
48
Reference Parameters
You can declare reference parameters:
void add10( int &x) { is r x = x+10; e t e m a r a p } The add10(counter);
C++ Functions
e c n re e f e ar
49
C++ Functions
50