Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Programming Fundamentals:2014
Lecture 13
Functions
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Function
Declaration/Prototype
Function
prototype
provides
information to the compiler about the
structure of the function to be used
in a program.
Rules:
Usually placed at the beginning of source
file just before the main() function.
Must be ends with a semicolon.
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Function
Declaration/Prototype
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Example
The following function show that the
function show accept no parameter and
no return type.
void show(void);
The following function show that the
function sum accept integer parameter
and integer return type.
int sum(int);
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Example
The following function show that the
function add accept three integer
parameter and integer return type.
int add(int a, int b, int c);
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Function Definition
A set of statements that explains
what a function does is called
function definition.
Rules:
Usually placed after the main() function.
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Function Dfinition
The function definition consists of two
parts.
Function Header:
First line of function definition.
Similar to function prototype but with no semicolon.
Number of parameters and return type will be same
as prototype.
Function Body:
Asif Nawaz
Lecture 02:
Programming Fundamentals:2014
Function
Header
Function
Body
Asif Nawaz
10
Lecture 02:
Programming Fundamentals:2014
Function Call
The statement that activates a
function is known as function call.
A function is called with its name and
parameter list.
When Function is called then:
The control moves to the function that is called.
All statements in the function body are executed.
The control returns back to the calling function.
Asif Nawaz
11
Lecture 02:
Programming Fundamentals:2014
Calling
Function
Function
Calls
Asif Nawaz
main( )
{
.
.
Fun( );
.
.
Fun( );
}
Called Function
void Fun( )
{
..
..
}
12
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
13
Lecture 02:
Programming Fundamentals:2014
void show(void);
main()
{
show();
getch();
}
void show(void)
{
cout<<Programming is an interesting field;
}
Asif Nawaz
14
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that add two integer
numbers. the numbers should to the
function as argument.
Asif Nawaz
15
Lecture 02:
Programming Fundamentals:2014
}
Asif Nawaz
16
Lecture 02:
Programming Fundamentals:2014
Passing Parameters to a
Function
Asif Nawaz
17
Lecture 02:
Programming Fundamentals:2014
Pass by Value
A parameter passing mechanism in
which the value of actual parameter
is copied to formal parameter of
called function is known as pass by
value.
Asif Nawaz
18
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that input two
integer numbers in main( ) function,
passes these numbers to function.
The function displays the maximum
number.
Asif Nawaz
19
Lecture 02:
Programming Fundamentals:2014
main()
{
int x, y;
cin>>x>>y;
max(x,y);
getch();
{
if(a>b);
{
cout<<Max number is "<<a<<endl;
else
cout<<Max number is "<<b<<endl;
}
Asif Nawaz
20
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that input a number
in main function and passes numbers
to a function. The function displays
table of that number.
Asif Nawaz
21
Lecture 02:
Programming Fundamentals:2014
cin>>num;
table(num);
getch();
Asif Nawaz
void table(int n)
{
for(int c=0; c<=10; c++)
{
cout<<n<<*<<c<<=<<n*c<<endl;
}
}
22
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that input a number
in main function and passes numbers
to a function. The function displays
factorial of that number.
Asif Nawaz
23
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that input two
numbers and one arithmetic operator
passes from main to function. The
function applies arithmetic operation
on that two numbers based on the
operator enter by the user using
switch statement.
Asif Nawaz
24
Lecture 02:
Programming Fundamentals:2014
cin>>x>>c>>y;
operation(x,y,c);
getch();
}
Asif Nawaz
25
Lecture 02:
Programming Fundamentals:2014
Pass by Refrence
A parameter passing mechanism in
which the value of actual parameter
is passed to the called function is
known as pass by value. The formal
parameter is not created separately
in the main memory.
Asif Nawaz
26
Lecture 02:
Programming Fundamentals:2014
Example
Asif Nawaz
27
Lecture 02:
Programming Fundamentals:2014
getch();
}
void swap(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
}
28
Lecture 02:
Programming Fundamentals:2014
80 or Above marks
Grade B
60 to 79 marks
Grade C
40 to 59 marks
Grade Fbelow 40 marks
Asif Nawaz
29
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
if(m>80)
return A;
else if (m>60)
return B;
else if (m>40)
return C;
else
return F;
}
30
Lecture 02:
Programming Fundamentals:2014
31
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
32
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
33
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
int square(int n)
{
return n * n;
}
int cube(int n)
{
return n * n * n;
}
34
Lecture 02:
Programming Fundamentals:2014
Scope of Variable
The variables can be declared inside
the main function, inside user
defined function.
The effect of the variables declared
in these places is different.
Based upon their effects, variables
are divided into two classes:
Local Variable
Global Variable
Asif Nawaz
35
Lecture 02:
Programming Fundamentals:2014
Local Variable
The variable declared inside the main
function or inside user defined
function is called Local Variable.
These variables may also be called
as Automatic Variables.
Asif Nawaz
36
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
37
Lecture 02:
Programming Fundamentals:2014
Global Variable
The variable declared outside the
main function or any other user
defined function is called global
Variable. These variables can be
accessed in any function at any time
during the execution of program.
Asif Nawaz
38
Lecture 02:
Programming Fundamentals:2014
Example
void fun()
{
g = g * 2;
int g;
}
void fun(void);
main()
{
cout<< Enter a number ;
cin>>g;
cout<<Value of g before function<<g;
fun();
cout<<Value of g after function<<g;
getch();
}
Asif Nawaz
39
Lecture 02:
Programming Fundamentals:2014
Default Parameter
The parameter that initialize during
function declaration is called default
parameter.
Asif Nawaz
40
Lecture 02:
Programming Fundamentals:2014
Output:
100
2
75
}
Asif Nawaz
41
Lecture 02:
Programming Fundamentals:2014
}
Asif Nawaz
42
Lecture 02:
Programming Fundamentals:2014
43
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that inputs five
integer in an array and passes the
array to a function. The function
display the value of the array.
Asif Nawaz
44
Lecture 02:
Programming Fundamentals:2014
int num[5], i;
cout<<Enter five Integer";
for(int i=0; i<=5; i++)
{
cin>>num[i];
}
}
}
show(num);
getch();
}
Asif Nawaz
45
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that inputs five
integer in an array and passes the
array to a function. The function
counts the even numbers in the
array. And return the result to main
function
where
the
result
is
displayed.
Asif Nawaz
46
Lecture 02:
Programming Fundamentals:2014
cin>>num[i];
}
n = even(num);
if(array[j]%2==0)
{
e++;
}
Cout<<The array
contain<<n<<even number;
getch();
}
Asif Nawaz
}
}
PMAS UAAR University Institute of Information Technology
47
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
48
Lecture 02:
Programming Fundamentals:2014
Example
Write a program that declares a
structure to store marks and grade. It
defines structure variable and input
values. It passes the variable to a
function that show its contents.
Asif Nawaz
49
Lecture 02:
Programming Fundamentals:2014
void show(Test p)
{
cout<<Marks<<p.marks<<endl;
cout<<Grade<<p.grade<<endl;
}
50
Lecture 02:
Programming Fundamentals:2014
Asif Nawaz
51
Lecture 02:
Programming Fundamentals:2014
cout<<Name<<x.name<<endl;
cout<<Population<<p.population;
else
cout<<Name<<y.name<<endl;
cout<<Population<<y.population;
fun(a,b);
getch();
}
Asif Nawaz
52
Lecture 02:
Programming Fundamentals:2014
Function Overloading
Function with same name but with
different parameters value is called
function overloading.
The function with same name may
be different with one of the following
ways.
Number of parameters
Type of parameter
Sequence of parameter
Asif Nawaz
53
Lecture 02:
Programming Fundamentals:2014
void line();
void line(int n);
void line(int n, char c);
main()
{
struct Test
{
line()
line(3);
line(5, @);
getch();
}
void line();
{
int i;
for(i=1; i<=10; i++)
{
cout<< * <<endl;
}
Asif Nawaz
void line(n);
{
int i;
for(i=n; i<=10; i++)
{
cout<< * <<endl;
}
54