1 / 21

Group Presentation

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

davina
Download Presentation

Group Presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Group Presentation Topic: Functions Subject: Introduction to Programming

  2. Group Members Roll # Name 921 M. Arbaz Afzal 922 S. Urooj Arshad 923 M. Umer 924 M. Ayzed Mirza 925 M. Adnan

  3. Topic Outline • Functions • Passing Arguments to Functions • Reference Arguments • Inline Functions & Default Arguments • Returning Values from Functions • Returning By Reference • Questions & Answers • Exit

  4. 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

  5. Functions • Simple Functions • The Function Declaration • Calling the Function • The Function Definition • Comparison with Library Functions • Eliminating the Declaration

  6. 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<<‘*’; }

  7. Library Functions Few examples of Library Function are: • clrscr (); • getch (); • sqrt (100); • pow (5, 2); etc.

  8. 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

  9. Passing Arguments to Functions • Passing Constants • Passing Variables

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

  11. 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

  12. 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.

  13. 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

  14. Inline Functions main() main() fnct() fnct (); fnct (); Repeated code Repeated code placed in function placed inline

  15. Inline Syntax There are changes in two places: • In Prototype inline void fnct (); • In Function’s Definition inline void fnct () { ………… ………… }

  16. 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

  17. 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); }

  18. The return Statement return (kilograms); Back

  19. 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

  20. Questions & Answers Please don’t ask difficult questions.

  21. Thank you For being with me.

More Related