CS1201:
Programming Language 2
By:Nouf Aljaffan
Edited by : Nouf Almunyif
Function I
Global VS Local variables
• local variable
▫ Variables that declared inside a function
▫ Declared inside any block of code
• Global variables
▫ Opposite of local the known throughout the entire
program
Global VS Local variables
#include < iostream>
Using namespace std;
Global VS Local variables
I is 5
I is 100
I is 5
#include < iostream>
Using namespace std;
void main() {
int i=4; int j=10;
i++;
if (j > 0)
cout<<“ i is “<<i<<endl;
if (j > 0)
{ int i=100;
/* 'i' is defined and so local to * this block */
Cout << "i is” << i;
} //end if
cout<<"i is “<<i;
} //end main
Function
• The function are reusable.
• functions may be called Procedures or Routines and
in object oriented programming called Methods .
• Save programmers’ time.
Prototype VS. Declaration
prototype : normally placed before the start of main()
but must be before the function definition.
General form :
function_return_type function_name (type
parameter1, type parameter2,…, type parameterN) ;
EX:
void Factorial (int x); // prototype -- x is a parameter
Prototype VS. Declaration
Prototype :
void foo(int x); // prototype -- x is a parameter
• Declaration
• void foo(int x) // declaration -- x is a parameter
{
Statement
.
.
.
}
EX:
#include<iostream>
using namespace std;
int square (int x)
{
return x*x;
}
Void main ( )
{
int number;
cout<<"Enter any number to Calculate the square of this number ";
cin>>number;
cout<<endl;
cout<<"the square of "<<number<<" is " <<square(number)<<endl;
}
EX:
#include<iostream>
using namespace std;
int square (int x) ;
Void main ( )
{
int number;
cout<<"Enter any number to Calculate the square of this number ";
cin>>number;
cout<<endl;
cout<<"the square of "<<number<<" is " <<square(number)<<endl;
}
int square (int x)
{ return x*x; }
Finding Errors in Function Code
int sum(int x, int y)
{
int result;
result = x+y;
}
this function must return an integer value as
indicated in the header definition
(return result;) should be added
Finding Errors in Function Code
int sum (int n)
{ if (n==0)
return 0;
else
n+(n-1);
}
the result of n+sum(n-1) is not returned;
sum returns an improper result, the else part
should be written as:else return n+(n-1);
Finding Errors in Function Code
void f(float a);
{
float a;
cout<<a<<endl;
}
; found after function definition header.
redefining the parameter a in the function
void f(float a)
{
float a2 = a + 8.9;
cout <<a2<<endl;
}
Finding Errors in Function Code
void product(void)
{
int a, b, c, result;
cout << “enter three integers:”;
cin >> a >> b >> c;
result = a*b*c;
cout << “Result is” << result;
return result;
}
According to the definition it should
not return a value , but in the block
(body) it did & this is WRONG.
Remove return Result;
Parameters vs Arguments
• Up until now, we have not differentiated between
function parameters and arguments.
• In common usage, the terms parameter and argument
are often interchanged.
▫ A function parameter is a variable declared in the
prototype or declaration of
▫ An argument is the value that is passed to the
function in place of a parameter.
methods of passing
• There are 2 primary methods of passing
arguments to functions:
▫ pass by value,
▫ pass by reference,
Passing arguments by value
• By default, arguments in C++
are passed by value.
• When arguments are passed
by value, a copy of the
argument is passed to the
function.
void foo(int y)
{
cout << "y = " << y << endl;
}
int main()
{
foo(5); // first call
int x = 6;
foo(x); // second call
foo(x+1); // third call
return 0;
}
Passing arguments by value
• Because a copy of the
argument is passed to
the function, the
original argument
can not be modified
by the function.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
void foo(int y)
{
cout << "y = " << y << endl;
y = 6;
cout << "y = " << y << endl;
} // y is destroyed here
int main()
{
using namespace std;
int x = 5;
cout << "x = " << x << endl;
foo(x);
cout << "x = " << x << endl;
return 0;
}
Adv. And DisAdv. Of Passing by value
Advantages
▫ Arguments passed by value
can be:
variables (eg. x),
literals (eg. 6),
or expressions (eg. x+1) or
(eg foo()).
▫ Arguments are never
changed by the function
being called, which prevents
side effects.
Disadvantages
Copying large structs or
classes can take a lot of time to
copy, and this can cause a
performance penalty,
especially if the function is
called many times.
© Copyright 2026 Paperzz