C++ programming function
Upcoming SlideShare
Loading in...5
×
 

C++ programming function

on

  • 548 views

summary

summary

Statistics

Views

Total Views
548
Views on SlideShare
537
Embed Views
11

Actions

Likes
0
Downloads
15
Comments
0

3 Embeds 11

http://tlwave.blogspot.com 8
http://www.blogger.com 2
http://www.tlwave.blogspot.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    C++ programming function C++ programming function Presentation Transcript

    • Function
    • Introduction Use of function  Calling a formula defined by user  Input & Output depends on type of function Not included in main body Can be called many times
    • Function Prototype After #include<iostream> Before int main() Example 1 # include <iostream> void FindMax(int, int); //prototype using namespace std; int main()
    • 4Calling FunctionExample 2int main(){ int num1, num2; cout<<"Please enter a number :"; cin>> num1; cout<<"Great!nPlease enter a second number :"; cin>> num2; FindMax(num1, num2); //calling function system ("PAUSE"); return 0;}
    • Defining Function Outside main body Example 3 void FindMax(int x, int y){ int maxNum; if (x>=y) maxNum=x; else maxNum=y; cout<<"Maximum number is : "<<maxNum <<endl; }
    • Using reference variable The Prototype:  void FindMax(double, double, double&); The Calling:  FindMax(num1, num2, maxNum); The Defining:  void FindMax(double x, double y, double& maxNum) maxNum: a value edited in main function
    • • Compare Example 2Example 4int main(){ double num1, num2, maxNum; cout<<"Please enter a number :"; cin>> num1; cout<<"Great!nPlease enter a second number :"; cin>> num2; FindMax(num1, num2, maxNum); //calling system ("PAUSE"); return 0;} Go to DevC++
    • Looping Function Just insert function in loop to call as needed Looping with returned value :  Declare return value  Send value to function Write a loop  Example :  Using while loop to keep comparing numbers
    • Example 5char next;while(true){ cout<<"n(n to exit)Compare with next number? :"; cin>> next; if(next==n) break; cout<<"Okay!nPlease enter the next number :"; cin>> num1; num2 = maxNum; FindMax(num1, num2, maxNum); }
    • END