1
Chapter IV
Lecturer: Hong Mom
Objec9ve
Lecturer: Hong Mom 2
A=er completely this chapter, you will be able to: • Define func9on • Define Scope role • Construct func9on headers and prototypes • Call by value and Call by reference • Define storage class specifiers
Func9on • What is Func9on? A func&on is a block of code that performs a calcula9on and returns a value. Once a func9on has been wriNen to play a par9cular role it can be called upon repeatedly throughout the program. • Also called a subrou&ne, procedure, or method
Lecturer: Hong Mom 3
Func9on
• Func9on is normally consist these factors as below
type name (parameter1, paramter2,…) { statements;
}
Lecturer: Hong Mom 4
Type Name Parameters Statements
Func9on
• type is the data type specifier of the data returned by the func9on.
• name is the iden9fier by which it will be possible to call the func9on.
• parameters (as many as needed): Each parameter consists of a data type specifier followed by an iden9fier, like any regular variable declara9on
Lecturer: Hong Mom 5
(for example: int x) and which acts within the func9on as a regular local variable. They allow to pass arguments to the func9on when it is called. The different parameters are separated by commas. • statements is the func9on's body. It is a block of statements surrounded by braces { }.
Lecturer: Hong Mom 6
Func9on
Example1
Lecturer: Hong Mom 7
#include<iostream.h> int sum2Num(int a, int b) {
int s; s = a + b; return (s);
} int main(){
int r; r = sum2Num(5,8); cout << “The result is “ << r; return 0;
}
The result is 13
Example2
Lecturer: Hong Mom 8
• Way1 #include <iostream.h> void displayNum(int num) {
num = 0; cout << "num = " << num << '\n';
} int main (void) {
int x = 10; displayNum (x); cout << "x = " << x << '\n'; return 0;
}
Output: num = 0; x = 10;
Example2
Lecturer: Hong Mom 9
• Way2 #include <iostream.h> void displayNum(int num); //Func9on Declara9on or Prototype int main (void) {
int x = 10; displayNum (x); cout << "x = " << x << '\n'; return 0;
} void displayNum(int num) {
num = 0; cout << "num = " << num << '\n';
}
Global and Local Scope
• Global Scope -‐ Everything defined at the program scope level (i.e., outside func9ons and classes) is said to have a global scope -‐ Can be accessed by any func9on defined below the declara9on • Local Scope -‐ Each block in a program defines a local scope . -‐ In general variables declared inside a block are accessible only in that block
Lecturer: Hong Mom 10
Scope of variables
Lecturer: Hong Mom 11
#include <iosteam.h> int x; char ch; main() {
int age; float number;
cout << “Enter your age: “; cin >> age; …
}
Global variables
Local variables
Call of Func9on
Func&on Call by value
#include <iostream.h> #include<iostream.h> void swap(int x, int y){
int temp = x; x = y; y = temp;
}
Func&on Call by Reference
#include <iostream.h> #include<iostream.h> void swap(int &x, int &y){
int temp = x; x = y; y = temp;
}
Lecturer: Hong Mom 12
Call of Func9on
Func&on Call by value a:10 and b: 20
void main() { int a = 10; int b = 20; swap(a, b); cout<<”a: ” << a<<” and b: ” <<b; }
Func&on Call by Reference a:20 and b: 10
Lecturer: Hong Mom 13
Storage class specifiers
• A storage class specifier is used to refine the declara9on of a variable in memory.
• The storage class specifiers in C++ are: – Auto – Register – Sta9c – Extern
Lecturer: Hong Mom 14
Auto Variables
Because the life9me of a local variable is limited and is determined automa9cally, these variables are also called automa&c. The storage class specifier auto may be used to explicitly specify a local variable to be automa9c. For example: void showMe(void) {
auto int xyz; // same as: int xyz; //... } This is rarely used because all local variables are by default automa9c.
Lecturer: Hong Mom 15
Register variable
• Used to indicate to the compiler that the variable should be stored in a register if possible.
• The scope of register variables is local to the block in which they are declared.
• fast For example: register int var;
Lecturer: Hong Mom 16
Sta9c variable
• Preserve the value for a par9cular variable upon re-‐entry into the same func9on.
For example: void sta9c_func9on_example() { sta9c int x = 0; //variable for C++ tutorial example x++; cout << x <<endl; }
Lecturer: Hong Mom 17
Extern variable
• Used to specify that the variable is declared in a different file.
• Used to declare variables of global scope in C++ projects
• When the keyword extern is used, the compiler will not allocate memory for the variable.
Lecturer: Hong Mom 18