Professional Documents
Culture Documents
• Introduction
• Function Prototype
• Function call
• Function Definition
• Passing variables to function
• Returning variable from function
• Scope Of variables
• Global variables
• Static variable
1 (C) Copyright 2020 by Dr.Abeer El_korany
Introduction
Modular Programming
• Modular programming:
– breaking a program up into smaller, manageable
functions or modules
• Software reusability
– Call function multiple times
• Functions call
• Invoke the function
• Function name and information (arguments) it needs
• Function definition
– Only written once
– Hidden from other functions
Method2
#include <library>
void myfunction()
{
Here-Is-The-Body-Of-My-Function;
}
void main()
{
myfunction();
}
✓ The return value of the invoked function is used as the value of the
invocation expression
#include <iostream>
using namespace std;
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << "\n ";
cout << "The second result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The third result is " << z << "\n ";
return 0;
}
21 (C) Copyright 2020 by Dr.Abeer El_korany
#include <iostream>
using namespace std; Void Function
void divide(double, double);
int main()
{
double num1, num2;
cout << "Enter two numbers and I will divide the first\n";
cout << "number by the second number: ";
cin >> num1 >> num2;
divide(num1, num2);
return 0;
}
/*************************Definition of function divide*****************/
void divide(double arg1, double arg2)
{ if (arg2 == 0.0)
cout << "Sorry, I cannot divide by zero.\n";
else
cout << "The result is " << (arg1 / arg2) << endl;
}
• File scope
• Defined outside a function, known in all functions
• Global variables, function definitions and prototypes
• Function scope
• Can only be referenced inside defining function.
• Local variables, known only in the function in which
they are defined
• Block scope
• A block is a list of statements within left curly braces{
begins at declaration, ends at right brace }
29 (C) Copyright 2020 by Dr.Abeer El_korany
Local and Global Variables
int main()
{
float p ; // Local to main() only.
cout << i << ", " << p << " \n "; // Prints global I and local p.
i++;
funagain(); // Calls next function.
cout << i << ", " << p << " \n "; // Prints global I and local p.
return 0;
}
float z = 10.0; // Global variable because it’s defined before a function.
void funagain()
{
int j = 5; // Local to only pr_again().
cout << j << ", " << z; // This can’t print p!.
cout << ", " << i << " \n ";
35 i++; (C) Copyright 2020 by Dr.Abeer El_ko
Static variables
• By default, all local variables are automatic, meaning
that they are erased when their function ends.
int main()
{
for (int count = 0; count < 5; count++)
showStatic();
return 0;
}
void showStatic()
{
static int statNum;
cout << "statNum is " << statNum << endl;
statNum++;
} (C) Copyright 2020 by Dr.Abeer El_korany
38
Static variables (cont.)
#include <iostream>
using namespace std;
void stat();
int main()
{ int i;
for (i=0;i<5; ++i)
stat();
return 0;
}
void stat()
{
int autovar = 0;
static int stvar ;
cout<< \n auto = ”<< autovar << “ static = “<< stvar <<endl;
autovar ++;
stvar++;
(C) Copyright 2020 by Dr.Abeer El_korany
} 39
Using of Static Variable (cont.)
#include <iostream>
using namespace std;
void showStatic(); void shownum();
int main()
{
for (int count = 0; count < 5; count++)
{ showStatic();
shownum();}
return 0;
}
void shownum()
{ int Num =1;
cout << "Num is " << Num << endl;
Num++;
}
void showStatic()
{ static int statNum=1;
cout << "statNum is " << statNum << endl;
statNum++;
} (C) Copyright 2020 by Dr.Abeer El_korany
40