Fundamentals of Computer Programming: Functions - II
Fundamentals of Computer Programming: Functions - II
Lecture 11
Functions – II
Course Instructor:
Quratulain Shafi Institute of Geographical Information Systems
Persistence needs Preparation &
Faith
Road Map for Today
• Recap
• Parameters & Arguments
• Default Values for Function Parameters
• Scope Rules
Recap
• C++ Function syntax -How many components?
• Where to define the functions in C++?
• When & why do we use function prototype?
• Depending on whether a function is predefined
or created by programmer; how many types of
functions are there?
Recap – Example
#include <iostream>
using namespace std; No. of
parameters?
int absolute(int a)
{
int r;
r = abs(a);
?
return r;
}
Function
int main () Prototype?
{
int z;
z = absolute (-5);
output?
cout << "The result is " << z;
}
Recap – An Exercise
• Write a function called DisplayMessage() which
when called from main() displays the message
“It’s a beautiful day today!”
Parameters Arguments
int absolute(int a)
{
int r;
r = abs(a);
return r;
}
Gives an error
Error description
Default Values – Scenario I
#include <iostream>
using namespace std;
}
r = a + b;
return r; IMPORTANT FACT NO 1
If you missNow
a parameter,
what will happen if the compiler assumes
int main () we miss an argument here?
{ that the rightmost parameter is missing.
int z;
z = addition (5);
cout << "The result is " << z;
}
Default Values – Scenario III
#include <iostream>
using namespace std;
}
r = a + b;
return r; IMPORTANT FACT NO 2
You cannot miss a default argument in
int main () Now What?
{
int z;
between two arguments.
z = addition (5,3,1);
cout << "The result is " << z;
}
Default Values – Scenario VI
#include <iostream>
In this case, c and d should also be assigned a default values.
using namespace std;
}
r = a + b;
return r; IMPORTANT FACT NO 3
You cannot miss a default argument after
int main () Now What?
{ the occurrence of a default argument.
int z;
z = addition (5,3,1);
cout << "The result is " << z;
}
Single Default Argument
int main ()
{
// Local variable declaration
int a, b, c;
// initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Local Variables – Example
#include <iostream>
using namespace std;
int someFunction()
{
int a, b, c; // Local variable declaration
// initialization
a = 10;
b = 20;
c = a + b;
return c;
}
int main ()
{
cout << someFunction();
cout << a << b;
}
Global Variables
• Defined usually on top of the program.
• Will hold their value throughout the life-time
of a program.
• Can be accessed by any function after
declaration of global variables.
Global Variables – Example
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration
int a, b;
// Initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
Global Variables – Example
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration
int a, b;
// Initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
Global Variables – Example
#include <iostream>
using namespace std;
void display(int g) {
cout << g << endl;
}
int main () {
// Local variable declaration
int a, b;
cout << g << endl;
display(g);
// Initialization
a = 10; b = 20;
g = a + b;
cout << g << endl;
}
Local & Global Variables – Naming
int main ()
{
// Local variable declaration
int g = 10;
cout << g;
return 0;
}