210 likes | 373 Views
Group Presentation. Topic: Functions Subject: Introduction to Programming. Group Members. Roll # Name 921 M. Arbaz Afzal 922 S. Urooj Arshad 923 M. Umer 924 M. Ayzed Mirza 925 M. Adnan. Topic Outline. Functions Passing Arguments to Functions
E N D
Group Presentation Topic: Functions Subject: Introduction to Programming
Group Members Roll # Name 921 M. Arbaz Afzal 922 S. Urooj Arshad 923 M. Umer 924 M. Ayzed Mirza 925 M. Adnan
Topic Outline • Functions • Passing Arguments to Functions • Reference Arguments • Inline Functions & Default Arguments • Returning Values from Functions • Returning By Reference • Questions & Answers • Exit
Functions Semicolon void func();Function declaration Return type void main() { Return type Semicolon func();Function call }No semicolon Return typevoid func()Declarator { Function Function bodydefinition } No semicolon
Functions • Simple Functions • The Function Declaration • Calling the Function • The Function Definition • Comparison with Library Functions • Eliminating the Declaration
Simple Functions #include<constream.h> void starline(); void main() { clrscr(); starline(); cout<<“\nEnter your name?\n”; starline(); cout<<“\nEnter your roll #”; getch(); } void starline() { for(int j=0; j<25; j++) cout<<‘*’; }
Library Functions Few examples of Library Function are: • clrscr (); • getch (); • sqrt (100); • pow (5, 2); etc.
Eliminating the Declaration #include<constream.h> void starline() { for(int j=0; j<25; j++) cout<<‘*’; } void main() { clrscr(); starline(); cout<<“\nEnter your name?\n”; starline(); cout<<“\nEnter your Roll #”; getch(); } Back
Passing Arguments to Functions • Passing Constants • Passing Variables
Passing Constants #include<constream.h> void repchar (char, int); void main() { clrscr(); repchar (‘-’, 23); cout<<“\nGC University\n”; repchar (‘=‘, 3); cout<<“\nCS Department”; getch (); } void repchar (char ch, int n) { for (int i=0; i<n; i++) cout<<ch; }
Passing Variables #include<constream.h> void repchar (char, int); void main () { clrscr (); char chin; int nin; cout<<“Enter a character= “; cin>>chin; cout<<“Enter number of times to repeat it= “; cin>>nin; repchar (chin, nin); getch (); } void repchar (char ch, int n) { for (int i=0; i<n; i++) cout<<ch; } Back
Reference Suppose, • a= variable name • *a= value at ‘a’ • &a= address of ‘a’ What is ‘&’? This is the reference. It provides a different name for a variable.
Reference Arguments #include<constream.h> void add (int&, int&); void main() { clrscr(); int a, b; cin>>a>>b; add (a, b); getch (); } void add (int &c, int &d) { int e=c+d; cout<<“Sum= “<<e; } Back
Inline Functions main() main() fnct() fnct (); fnct (); Repeated code Repeated code placed in function placed inline
Inline Syntax There are changes in two places: • In Prototype inline void fnct (); • In Function’s Definition inline void fnct () { ………… ………… }
Default Arguments #include<constream.h> void repchar (char=‘*’, int=25); void main() { clrscr (); repchar (); repchar (‘=‘); repchar (‘+’, 10); getch (); } void repchar (char ch, int n) { for (int j=0; j<n; j++) cout<<ch; cout<<endl; } Back
Returning Values from Functions #include<constream.h> float lbstokgs (float); void main() { clrscr(); float lbs, kgs; cout<<“Your weight in pounds= “; cin>>lbs; kgs= lbstokg (lbs); cout<<“Weight in Kg= “<<kgs; getch(); } void lbstokgs (float pounds) { float kilograms= .45* pounds; return (kilograms); }
The return Statement return (kilograms); Back
Returning by Reference #include<constream.h> int swap (int&, int&); void main() { clrscr(); int a, b; cout<<“First value= “; cin>>a; cout<<“Second value= “; cin>>b; swap (a, b); cout<<“Value after swapping= “<<a; cout<<“\nValue after swapping= “<<b; getch (); } int swap (int &c, int &d) { int e=c; c=d; d=e; return (c, d); } Back
Questions & Answers Please don’t ask difficult questions.
Thank you For being with me.