Sei sulla pagina 1di 54

Lecture 02:

Programming Fundamentals:2014

Lecture 13
Functions

Asif Nawaz

PMAS UAAR University Institute of Information Technology

Lecture 02:

Programming Fundamentals:2014

User Defined Function

Asif Nawaz

PMAS UAAR University Institute of Information Technology

Lecture 02:

Programming Fundamentals:2014

A user-defined function consists of


the following:
Function Declaration may also be called as
Function Prototype
Function Definition

Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

Lecture 02:

Programming Fundamentals:2014

Function
Declaration/Prototype

Function Declaration consists of the


following parts.
Function Name
Function Return Type.
Number and Types of Parameters.
Where:
Return Type indicate the type of value that will be returned
by function.
Parameters are the values that are provided to a function
when the function is called.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

The set of statements which are executed inside


function.
The body will always appear after function declarator
and statements are written in curly braces.
PMAS UAAR University Institute of Information Technology

Lecture 02:

Programming Fundamentals:2014

Syntax Function Definition


Return-Type Function-Name(Parameter)
{
Statement 1;
Statement 2;
.
.
.
Statement N;

Function
Header

Function
Body

Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

11

Lecture 02:

Programming Fundamentals:2014

Calling
Function

Function
Calls

Asif Nawaz

main( )
{
.
.
Fun( );
.
.
Fun( );
}

Called Function
void Fun( )
{
..
..
}

PMAS UAAR University Institute of Information Technology

12

Lecture 02:

Programming Fundamentals:2014

Write a program that display a


message
Programming is an
Interesting Field on screen by using
function.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

13

Lecture 02:

Programming Fundamentals:2014

void show(void);
main()
{
show();
getch();
}
void show(void)
{
cout<<Programming is an interesting field;
}
Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

15

Lecture 02:

Programming Fundamentals:2014

void sum(int , int );


main()
{
sum(10, 15);
cout<<"Ok";
getch();
}
void sum(int x, int y)
{
int s = x + y;
cout<<"Sum is = "<<s<<endl;

}
Asif Nawaz

PMAS UAAR University Institute of Information Technology

16

Lecture 02:

Programming Fundamentals:2014

Passing Parameters to a
Function

Two ways to pass values to functions


Parameter:
Pass by value.
Pass by Reference.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

19

Lecture 02:

Programming Fundamentals:2014

void max(int a,int b );

main()
{
int x, y;

void max(int a, int


b)

cout<<Enter two numbers";

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

21

Lecture 02:

Programming Fundamentals:2014

void table(int n);


main()
{
int num;
cout<<Enter number for
table";

cin>>num;
table(num);
getch();

Asif Nawaz

void table(int n)
{
for(int c=0; c<=10; c++)
{
cout<<n<<*<<c<<=<<n*c<<endl;

}
}

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

24

Lecture 02:

Programming Fundamentals:2014

void operation(int a, int b,


char ope);
{
int x, y;
char c;
cout<<Enter first number opeartor
and second numbers";

cin>>x>>c>>y;
operation(x,y,c);
getch();
}

Asif Nawaz

void operation(int a, int b, char ope)


{
switch(ope)
{
case +:
cout<< a<<+<<b<<a+b;
break;
case -:
cout<< a<<-<<b<<a-b;
break;
case *:
cout<< a<<*<<b<<a*b;
break;
.
.
.
default:
cout<<Invalid Operator!;
}

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

26

Lecture 02:

Programming Fundamentals:2014

Example

Program to swap values using pass by


reference.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

27

Lecture 02:

Programming Fundamentals:2014

void swap(int &X, int &y);


main()
{
int a,b;
cout<<Enter values;
cin>>a>>b;
cout<<Values before swap:;
cout<<a =<<a<<endl;
cout<<b =<<b<<endl;
swap(a,b);
getch();
cout<<Values after swap:;
cout<<a =<<a<<endl;
cout<<b =<<b<<endl;
Asif Nawaz

getch();
}
void swap(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
}

PMAS UAAR University Institute of Information Technology

28

Lecture 02:

Programming Fundamentals:2014

Returning Value from a


Function

Write a program that inputs marks in


main function and passes these marks
to a function. The function finds grade
of a student on the bases of following
criteria.
Grade A

80 or Above marks

Grade B
60 to 79 marks
Grade C
40 to 59 marks
Grade Fbelow 40 marks
Asif Nawaz

PMAS UAAR University Institute of Information Technology

29

Lecture 02:

Programming Fundamentals:2014

char grade(int m);


main()
{
int marks;
char g;
cout<<Enter marks;
cin>>marks;
g = grade(marks);
cout<<Your grade is<<g;
getch();
Char grade(int m)
{

Asif Nawaz

if(m>80)
return A;
else if (m>60)
return B;
else if (m>40)
return C;
else
return F;
}

PMAS UAAR University Institute of Information Technology

30

Lecture 02:

Programming Fundamentals:2014

Write a program that inputs base and


height in main function and passes
them to a function. The function finds
the area of a triangle and return it to
a main function, where it display on
screen.
Area is = (Base * height)
Asif Nawaz

PMAS UAAR University Institute of Information Technology

31

Lecture 02:

Programming Fundamentals:2014

float triangle(int b, int h);


main()
{
int base, height;
float area;
cout<<Enter base;
cin>>base;
cout<<Enter height;
cin>>height;
area = triangle(base, height);
cout<<Area is<<area;
getch();
}

Asif Nawaz

float area(int b, int h)


{
float a;
a = 0.5 * b * h;
return a;
}

PMAS UAAR University Institute of Information Technology

32

Lecture 02:

Programming Fundamentals:2014

Write a program that inputs two


integer. It passes one integer to the
function that calculate and return its
square. It passes second integer to
another function that calculate and
return its cube. The main function
add both values and return its result.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

33

Lecture 02:

Programming Fundamentals:2014

int square(int n);


int cube(int n);
main()
{
int a, b, sum;
cout<<Enter first Integer;
cin>>a;
cout<<Enter Second Integer;
cin>>b;
sum = sqr(a) + cube(b);
cout<<Result is<<sum;
getch();
}

Asif Nawaz

int square(int n)
{
return n * n;
}
int cube(int n)
{
return n * n * n;
}

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

36

Lecture 02:

Programming Fundamentals:2014

void sum(int , int );


main()
{
int a = 50;
int b = 60;
s = sum(10, 15);
cout<<Sum is =<<sum;
cout<<"Ok";
getch();
}

Asif Nawaz

void sum(int x, int y)


{
int s = x + y;
return s;

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

39

Lecture 02:

Programming Fundamentals:2014

Default Parameter
The parameter that initialize during
function declaration is called default
parameter.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

40

Lecture 02:

Programming Fundamentals:2014

void Test(int n = 100);


main()
{
Test();
Test(2);
Test(75);
getch();
}
void Test(int n)
{
cout<<n= "<<n<<endl;

Output:
100
2
75

}
Asif Nawaz

PMAS UAAR University Institute of Information Technology

41

Lecture 02:

Programming Fundamentals:2014

void add(int v1=5, int v2 = 10);


main()
{
int v3 = v1 + v2;
cout<<The sum of two num =<<v3;
getch();
}
Output:
void Test(int n)
The sum of two num = 15
{
The sum of two num = 3
add();
The sum of two num = 11
add(1,2);
add(1);

}
Asif Nawaz

PMAS UAAR University Institute of Information Technology

42

Lecture 02:

Programming Fundamentals:2014

Functions and Arrays


An array can be passed to a function
as a parameter.
When an array is passed as a
parameter to a function, only the
address of first element of the array
is passed.
An array is passed by reference not
by value.
Asif Nawaz

PMAS UAAR University Institute of Information Technology

void display(int array[]);

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

PMAS UAAR University Institute of Information Technology

44

Lecture 02:

Programming Fundamentals:2014

void show(int array[]);


main()
{

void show(int array[])


{
int j;

int num[5], i;
cout<<Enter five Integer";
for(int i=0; i<=5; i++)
{

for(int j=0; j<=5; j++)


{
cout<<array[j]<<endl;

cin>>num[i];

}
}

}
show(num);
getch();

}
Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

46

Lecture 02:

Programming Fundamentals:2014

int even(int array[]);


main()
{
int num[5], i, n;
cout<<Enter five Integer";
for(int i=0; i<=5; i++)
{

cin>>num[i];

int even(int array[])


{
int j, e;
e = 0;
for(int j=0; j<=5; j++)

}
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

Function and Structure


The structures can also be used with
function.
A function can accept structure as
parameter.
A function can also return a structure
variable.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

49

Lecture 02:

Programming Fundamentals:2014

void show(Test p);


main()
{
struct Test
{
int marks;
char grade;
};
Test t;
cout<<Enter marks;
cin>>t.marks;
cout<<Enter grade;
cin>>t.grade;
show(t);
getch();
Asif Nawaz

void show(Test p)
{
cout<<Marks<<p.marks<<endl;
cout<<Grade<<p.grade<<endl;
}

PMAS UAAR University Institute of Information Technology

50

Lecture 02:

Programming Fundamentals:2014

Write a program that declares a


structure to store country name and
population in million. It defines two
structure variable and inputs values.
It passes both variables to a function
that show the record of a country
with more population.

Asif Nawaz

PMAS UAAR University Institute of Information Technology

51

Lecture 02:

Programming Fundamentals:2014

void fun(pop x, pop y);


main()
{
struct pop
{
char name;
float people;
};
pop a,b;

void fun(pop x, pop y)


{
cout<<The country with
more population :;
{
if(x.pp>y.pp)
{

cout<<Enter Country Name


& population;
cin>>a.name>>a.people;
cout<<Enter Country Name
& population;
cin>>b.name>>b.people;

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

PMAS UAAR University Institute of Information Technology

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

PMAS UAAR University Institute of Information Technology

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

void line(int n, char c);


{
int I;
for(i=1; i<=n; i++)
{
cout<<c<<endl;
}

PMAS UAAR University Institute of Information Technology

54

Potrebbero piacerti anche