0% found this document useful (0 votes)
48 views

Fundamentals of Computer Programming: Functions - II

The document provides a summary of a lecture on functions in C++. It discusses parameters and arguments, and how default values can be provided for function parameters. It also covers scope rules for variables, explaining that variables can have local, global, or formal parameter scope depending on where they are declared. Local variables are only accessible within the block or function they are declared, while global variables are accessible anywhere after declaration.

Uploaded by

Mina Sam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Fundamentals of Computer Programming: Functions - II

The document provides a summary of a lecture on functions in C++. It discusses parameters and arguments, and how default values can be provided for function parameters. It also covers scope rules for variables, explaining that variables can have local, global, or formal parameter scope depending on where they are declared. Local variables are only accessible within the block or function they are declared, while global variables are accessible anywhere after declaration.

Uploaded by

Mina Sam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

CS-114

Fundamentals of Computer Programming

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!”

• Write a function called welcome() which takes


name of the user as parameter and when called
from main() displays the message
“Hello [nameOfUser]!”
Parameters & Arguments
Classified by Location

Parameters Arguments

Always appear in Always appear in


the function definition, a function call.
or function prototype.
Parameters & Arguments – Example
#include <iostream>
#include <cstdlib> Parameter
using namespace std;

int absolute(int a)
{
int r;
r = abs(a);
return r;
}

int main () Argument


{
int z;
z = absolute (-5);
cout << "The result is " << z;
}
Default Values for
Function Parameters
What do we mean by DEFAULT?
• Pre-established/ pre-defined value
• Software – default settings
• Cell phones – factory settings
Default values for Parameters
• In C++ programming, you can provide default
values for function parameters.

• The idea behind default argument is simple.


• If a function is called by passing argument/s,
those arguments are used by the function.
• But if the argument/s are not passed while
invoking/calling a function then, the default
values are used.
Default Values – Example (AddNumbers)
#include <iostream>
using namespace std;

int addition (int a, int b)


{
int r;
r = a + b; Parameters
return r;
}
Arguments
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
Default Values – Example (AddNumbers)
#include <iostream>
using namespace std;

int addition (int a, int b)


{
int r;
r = a + b; Integers a and b have not been given any values here
return r;
}
What will happen if we miss an argument here?
int main ()
{
int z;
z = addition (5);
cout << "The result is " << z;
}
Default Values – Example (AddNumbers)

Gives an error

Error description
Default Values – Scenario I
#include <iostream>
using namespace std;

int addition (int a = 0, int b = 0)


{
int r;
r = a + b; Give some value to variables in function definition
return r; and it will be called as default value of that variable
}

int main () Passing both argument values


{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
Default Values – Scenario II
#include <iostream>
using namespace std;

int addition (int a = 0, int b = 0)


{
int r;

}
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;

int addition (int a = 0, int b = 0)


{
int r;
r = a + b;
return r;
}
What if we omit
int main () both arguments?
{
int z;
z = addition();
cout << "The result is " << z;
}
Default Values – Scenario IV
#include <iostream>
using namespace std;
Only one variable
int addition (int a, int b = 0) has default value
{
int r;
r = a + b;
return r;
}

int main () Now What?


{
int z;
z = addition (5);
cout << "The result is " << z;
}
Default Values – Scenario V
#include <iostream>
In namespace
using this case, c should
std; also be assigned a default value.
int addition (int a, int b = 0, int c, int d = 4)
{
int r; The above function will not compile.

}
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;

int addition (int a, int b = 0, int c, int d)


{
int r; The above function will not compile.

}
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

• If you want a single default argument, make sure


the argument is the last one.

int addition (int a,int b,int c,int d = 2);


Scope Rules
(Variables)
Scope
A scope is a region of a program.

Scope defines where your variables


will be accessible
throughout your script.
Scope of Variables

Scope can be explained by looking at the


ways that international laws, national laws,
and local laws work together.
Scope of Variables
Broadly speaking there are three places, where
variables can be declared −
• Inside a function or a block which are called
local variables.
• In the header of function definition which are
called formal parameters/parameter-list.
• Outside of all functions which are called
global variables.
Local Variables
• Function Scope
• Block Scope

• They can be used only by statements that are


inside that function or block of code.
• Local variables are not known to functions
outside their own.
Local Variables – Example
#include <iostream>
using namespace std;

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;

// Global variable declaration


int g;

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;

// Global variable declaration & initialization


int g = 10;

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;

// Global variable declaration


int g = 10;

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

A program can have same name


for local and global variables
But
value of local variable inside a function
will take preference.
Local & Global Variables – Naming
#include <iostream>
using namespace std;

// Global variable declaration


int g = 20;

int main ()
{
// Local variable declaration
int g = 10;
cout << g;

return 0;
}

You might also like