8/16/2019 Lucky Lecture Yes Yes s
1/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
Lecture 0 ! Programming "un#amentals ! 0$%
Lecture $&
"unctions
$
8/16/2019 Lucky Lecture Yes Yes s
2/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
User 'e(ne# "unction
8/16/2019 Lucky Lecture Yes Yes s
3/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
• A user)#e(ne# function consists ofthe following!
– "unction 'eclaration may also *e calle# as"unction Prototy+e
– "unction 'e(nition
&
8/16/2019 Lucky Lecture Yes Yes s
4/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
"unction'eclaration,Prototy+e
"unction +rototy+e +rovi#esinformation to the com+iler a*out thestructure of the function to *e use#in a +rogram-
Rules! – Usually placed at the beginning of source
file just before the main() function. – Must be ends with a semicolon.
%
8/16/2019 Lucky Lecture Yes Yes s
5/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
"unction'eclaration,Prototy+e
• "unction 'eclaration consists of thefollowing +arts-
– "unction Name – "unction Return Ty+e- – Num*er an# Ty+es of Parameters-
.here !Return Ty+e in#icate the ty+e of value that will *e returne#*y function-Parameters are the values that are +rovi#e# to a functionwhen the function is calle#-
/
8/16/2019 Lucky Lecture Yes Yes s
6/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
The following function show that thefunction show acce+t no +arameter an#no return ty+e-
void show(void);
The following function show that thefunction sum acce+t integer +arameter
an# integer return ty+e-
int sum(int);
2
8/16/2019 Lucky Lecture Yes Yes s
7/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
The following function show that thefunction a## acce+t three integer+arameter an# integer return ty+e-
int add(int a, int b, int c);
3
8/16/2019 Lucky Lecture Yes Yes s
8/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
"unction 'e(nition
• A set of statements that e1+lainswhat a function #oes is calle#function #e(nition-
Rules! – Usually placed after the main() function.
4
8/16/2019 Lucky Lecture Yes Yes s
9/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
"unction '(nition
• The function #e(nition consists of two+arts-
• "unction 5ea#er! – "irst line of function #e(nition- – Similar to function +rototy+e *ut with no semicolon- – Num*er of +arameters an# return ty+e will *e same
as +rototy+e-
• "unction 6o#y! – The set of statements which are e1ecute# insi#e
function- – The *o#y will always a++ear after function #eclarator
an# statements are written in curly *races- 7
8/16/2019 Lucky Lecture Yes Yes s
10/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
Synta1 "unction 'e(nition
Return !ype "unction #ame($arameter)
%
&tatement ';
&tatement ;
.
.
.
&tatement #;
$0
"unction5ea#er
"unction6o#y
8/16/2019 Lucky Lecture Yes Yes s
11/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
"unction 8all
• The statement that activates afunction is 9nown as function call-
• A function is calle# with its name an#+arameter list-
• .hen "unction is calle# then! – The control moves to the function that is calle#- – All statements in the function *o#y are e1ecute#- – The control returns *ac9 to the calling function-
$$
8/16/2019 Lucky Lecture Yes Yes s
12/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
$
main: ; <===-===-"un: ;>===-
===-"un: ;> ?
voi# "un: ; <
==--==--
?
8alling"unction
"unction8alls
8alle# "unction
8/16/2019 Lucky Lecture Yes Yes s
13/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
.rite a +rogram that #is+lay amessage @Programming is anInteresting "iel# on screen *y usingfunction-
$&
8/16/2019 Lucky Lecture Yes Yes s
14/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
void show(void);
main()
%
show();getch();
void show(void)
%
cout**+ Programming is an interesting field” ;
$%
8/16/2019 Lucky Lecture Yes Yes s
15/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
.rite a +rogram that a## two integernum*ers- the num*ers shoul# to thefunction as argument-
$/
8/16/2019 Lucky Lecture Yes Yes s
16/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
$2
void sum(int , int );
main()
%
sum(' , '-);
cout** /0 ;getch();
void sum(int 1, int y)
%
int s 2 1 3 y;
cout** &um is 2 **s**endl;
8/16/2019 Lucky Lecture Yes Yes s
17/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
Passing Parameters to a"unction
• Two ways to +ass values to functionBsParameter!
» Pass *y value-» Pass *y Reference-
$3
$
8/16/2019 Lucky Lecture Yes Yes s
18/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
Pass *y Calue
A +arameter +assing mechanism inwhich the value of actual +arameteris co+ie# to formal +arameter ofcalle# function is 9nown as +ass *yvalue-
$4
0 i l 0$%
8/16/2019 Lucky Lecture Yes Yes s
19/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
$7
.rite a +rogram that in+ut twointeger num*ers in main: ; functionD+asses these num*ers to function-
The function #is+lays the ma1imumnum*er-
L 0 ! P i " # l ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
20/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
void ma1(int a,int b );
main()
%
int 1, y;cout**+4nter two numbers ;
cin55155y;
ma1(1,y);
getch();
void ma1(int a, intb)
%if(a5b);
% cout**+Ma1 number is **a**endl;
else cout**+Ma1 number is **b**endl;
0
L 0 ! P i " # l ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
21/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
$
.rite a +rogram that in+ut a num*erin main function an# +asses num*ersto a function- The function #is+laysta*le of that num*er-
L t 0 ! P i " # t l ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
22/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
void table(int n);
main()
%int num;
cout**+4nter number fortable ;
cin55num;
table(num);
getch();
void table(int n)
%for(int c2 ; c*2' ; c33)
%
cout**n**+6+**c**+2+**n6c**endl;
?
L t 0 ! P g i g " # t l ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
23/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
&
.rite a +rogram that in+ut a num*erin main function an# +asses num*ersto a function- The function #is+laysfactorial of that num*er-
Lect re 0 ! Programming " n#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
24/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
1am+le
.rite a +rogram that in+ut twonum*ers an# one arithmetic o+erator+asses from main to function- Thefunction a++lies arithmetic o+erationon that two num*ers *ase# on theo+erator enter *y the user using
switch statement-
%
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
25/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming "un#amentals ! 0$%
void operation(int a, int b, char ope)
%
switch(ope)
%
case 7389
cout** a**+3:**b**a3b;
brea0;
case 7 89
cout** a**+ :**b**a b;
brea0;
case 7689
cout** a**+6:**b**a6b;
brea0;
.
.
.
default9
cout**+ nvalid /perator
8/16/2019 Lucky Lecture Yes Yes s
26/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
Pass *y Refrence
2
A +arameter +assing mechanism inwhich the value of actual +arameteris +asse# to the calle# function is9nown as +ass *y value- The formal+arameter is not create# se+aratelyin the main memory-
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
27/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
1am+le
Program to swa+ values using +ass *yreference-
3
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
28/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
4
void swap(int =>, int =y);main()
%
int a,b;
cout**+4nter values:;
cin55a55b;cout**+?alues before swap9:;
cout**+a 2+**a**endl;
cout**+b 2+**b**endl;
swap(a,b);
getch();cout**+?alues after swap9:;
cout**+a 2+**a**endl;
cout**+b 2+**b**endl;
getch();
void swap(int =1, int =y)%int t;
t 2 1;1 2 y;y 2 t;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
29/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
Returning Calue from a"unction
.rite a +rogram that in+uts mar9s inmain function an# +asses these mar9sto a function- The function (n#s gra#eof a stu#ent on the *ases of followingcriteria-
Era#e A 40 or A*ove mar9sEra#e 6 20 to 37 mar9sEra#e 8 %0 to /7 mar9sEra#e "*elow %0 mar9s
7
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
30/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
&0
char grade(int m);
main()
%
int mar0s;
char g;
cout**+4nter mar0s:;
cin55mar0s;
g 2 grade(mar0s);
cout**+@our grade is:**g;
getch();
Ahar grade(int m)
%
if(m5B )return 7C8;
else if (m5D )return 7E8;
else if (m5F )
return 7A8;else
return 7"8;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
31/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
.rite a +rogram that in+uts *ase an#height in main function an# +assesthem to a function- The function (n#sthe area of a triangle an# return it toa main functionD where it #is+lay onscreen-
Crea is 2 G (Ease 6 height)
&$
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
32/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
&
float triangle(int b, int h);
main()
%
int base, height;
float area;
cout**+4nter base:;
cin55base;
cout**+4nter height:;
cin55height;
area 2 triangle(base, height);
cout**+Crea is:**area;
getch();
float area(int b, int h)%
float a;a 2 .- 6 b 6 h;return a;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
33/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
&&
.rite a +rogram that in+uts twointeger- It +asses one integer to thefunction that calculate an# return itssFuare- It +asses secon# integer toanother function that calculate an#return its cu*e- The main function
a## *oth values an# return its result-
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
34/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
&%
int sHuare(int n);
int cube(int n);
main()
%
int a, b, sum;
cout**+4nter first nteger:;
cin55a;
cout**+4nter &econd nteger:;
cin55b;
sum 2 sHr(a) 3 cube(b);
cout**+Result is:**sum;
getch();
int sHuare(int n)%
return n 6 n;
int cube(int n)
%return n 6 n 6 n;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
35/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
Lecture 0 ! Programming un#amentals ! 0$%
Sco+e of Caria*le
• The varia*les can *e #eclare# insi#ethe main functionD insi#e user#e(ne# function-
• The eGect of the varia*les #eclare#in these +laces is #iGerent-
• 6ase# u+on their eGectsD varia*lesare #ivi#e# into two classes!
• Local Caria*le• Elo*al Caria*le
&/
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
36/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
ectu e 0 ! og a g u a e ta s ! 0$%
Local Caria*le
• The varia*le #eclare# insi#e the mainfunction or insi#e user #e(ne#function is calle# Local Caria*le-
These varia*les may also *e calle#as Automatic Caria*les-
&2
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
37/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
g g $
&3
void sum(int , int );
main()
%
int a 2 - ;
int b 2 D ;s 2 sum(' , '-);
cout**+&um is 2+**sum;
cout** /0 ;
getch();
void sum(int 1, int y)%int s 2 1 3 y;return s;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
38/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
g g
Elo*al Caria*le
&4
• The varia*le #eclare# outsi#e themain function or any other user#e(ne# function is calle# glo*alCaria*le- These varia*les can *eaccesse# in any function at any time#uring the e1ecution of +rogram-
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
39/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
g g
1am+le
&7
int g;
void fun(void);
main()
%
cout**+ 4nter a number +;cin55g;
cout**+?alue of g before function:**g;
fun();
cout**+?alue of g after function:**g;
getch();
void fun()%
g 2 g 6 ;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
40/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
g g
'efault Parameter
• The +arameter that initialize #uringfunction #eclaration is calle# #efault+arameter-
%0
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
41/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
g g
%$
void !est(int n 2 ' );
main()
%
!est();
!est( );!est(I-);
getch();
void !est(int n)
%
cout**+n2 **n**endl;
Output:
1002
75
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
42/54
Asif Nawaz PMAS UAAR University Institute of Information Technology %
void add(int v'2-, int v 2 ' );main()
%
int vJ 2 v' 3 v ;
cout**+!he sum of two num 2+**vJ;getch();
void !est(int n)
%
add();add(', );
add(');
Output:
!he sum of two num 2 '-
!he sum of two num 2 J!he sum of two num 2 ''
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
43/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
"unctions an# Arrays
• An array can *e +asse# to a functionas a +arameter-
• .hen an array is +asse# as a+arameter to a functionD only thea##ress of (rst element of the arrayis +asse#-
• An array is +asse# *y reference not*y value-
void display(int arrayKL); %&
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
44/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
1am+le
.rite a +rogram that in+uts (veinteger in an array an# +asses thearray to a function- The function#is+lay the value of the array-
%%
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
45/54
Asif Nawaz PMAS UAAR University Institute of Information Technology %/
void show(int arrayKL);
main()
%int numK-L, i;
cout**+4nter five nteger ;for(int i2 ; i*2-; i33)
%
cin55numKiL;
show(num);getch();
void show(int arrayKL)
%
int j;for(int j2 ; j*2-; j33)
%
cout**arrayKjL**endl;
?
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
46/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
1am+le
%2
.rite a +rogram that in+uts (veinteger in an array an# +asses thearray to a function- The functioncounts the even num*ers in thearray- An# return the result to mainfunction where the result is
#is+laye#-
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
47/54
Asif Nawaz PMAS UAAR University Institute of Information Technology %3
int even(int arrayKL);main()
%int numK-L, i, n;
cout**+4nter five nteger ;
for(int i2 ; i*2-; i33)%
cin55numKiL;
n 2 even(num);Aout**+!he array
contain**n**+even number:;
getch();
int even(int arrayKL)
%
int j, e;
e 2 ;for(int j2 ; j*2-; j33)
%if(arrayKjL 22 )
%
e33;
?
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
48/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
"unction an# Structure
• The structures can also *e use# withfunction-
• A function can acce+t structure as+arameter-
• A function can also return a structurevaria*le-
%4
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
49/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
1am+le
.rite a +rogram that #eclares astructure to store mar9s an# gra#e- It#e(nes structure varia*le an# in+utvalues- It +asses the varia*le to afunction that show its contents-
%7
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
50/54
Asif Nawaz PMAS UAAR University Institute of Information Technology /0
void show(!est p);
main()
%
struct !est
%
int mar0s;
char grade;
;
!est t;
cout**+4nter mar0s:;
cin55t.mar0s;
cout**+4nter grade:;
cin55t.grade;
show(t);
getch();
void show(!est p)%cout**+Mar0s:**p.mar0s**endl;cout**+Nrade:**p.grade**endl;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
51/54
Asif Nawaz PMAS UAAR University Institute of Information Technology /$
.rite a +rogram that #eclares astructure to store country name an#+o+ulation in million- It #e(nes twostructure varia*le an# in+uts values-It +asses *oth varia*les to a functionthat show the recor# of a country
with more +o+ulation-
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
52/54
Asif Nawaz PMAS UAAR University Institute of Information Technology /
void fun(pop 1, pop y);
main()
%struct pop
%
char name;
float people;
;pop a,b;cout**+4nter Aountry #ame
= population:;
cin55a.name55a.people;
cout**+4nter Aountry #ame= population:;
cin55b.name55b.people;
fun(a,b);
getch();
void fun(pop 1, pop y)%cout**+!he country withmore population 9:;%
if(1.pp5y.pp)%cout**+#ame:**1.name**endl;cout**+$opulation:**p.population;else
cout**+#ame:**y.name**endl;cout**+$opulation:**y.population;
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
53/54
Asif Nawaz PMAS UAAR University Institute of Information Technology
"unction Hverloa#ing
• "unction with same name *ut with#iGerent +arameters value is calle#function overloa#ing-
• The function with same name may*e #iGerent with one of the followingways-
» Num*er of +arameters» Ty+e of +arameter» SeFuence of +arameter
/&
Lecture 0 ! Programming "un#amentals ! 0$%
8/16/2019 Lucky Lecture Yes Yes s
54/54
void line();
void line(int n);
void line(int n, c ar c);
main()
!struct "est!
line()line(#);
line(5, $ @ %);getc ();&
void line();!
int i;
for(i'1; i '10; i )
!cout * + * endl;
&
void line(n);
%
int i;
for(i2n; i*2' ; i33)
%
cout**+ 6 +**endl;
void line(int n, char c);
%
int ;
for(i2'; i*2n; i33)%
cout**c**endl;
Top Related