Lecture 9 - Lecture 9 Oct 2, 02 Functions Function is a...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 9 Oct 2, 02 Functions Function is a self contained block of program that performs a coherent task of some kind. Eg: Consider an automobile, it consists of various modules like the engine, transmission, braking, the body so on.... Each of these modules are linked and placed under the control of the driver, which can be compared to the main program. Each module can be considered a function. Each module can be individually constructed, tested, and found to be free of defects before it is installed in the final product. A C++ program must contain a main() function. In addition to this required function, C++ programs may also contain any number of additional functions. These additional functions are under the main() function. Simple Program with a function Eg 1 #include<iostream.h> void message(); // function prototype declaration int main() { message(); // calling the function message cout<<"I am in main() function"<<endl; return 0; } // the additional function message void message() // the function header line { cout<<"I am in function message"<<endl; // body of function } output: I am in function message I am in main() function Example 2 #include<iostream.h> int calsum(int, int, int) // function prototype declaration int main() { int a,b,c,sum; cout<<"Enter any three numbers"<<endll cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here return 0; } // The function calsum int calsum(int x, int y, int z) // function header { int d; d = x+y+z; return(d); } Example 3 #include<iostream.h> float calsum(int, float, int) // function prototype declaration int main() { int a,c; float b,sum; cout<<"Enter any three numbers"<<endll cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here return 0; } // The function calsum float calsum(int x, float y, int z) // function header { float d; d = x+y+z; return(d); } Example 4 #include<iostream.h> float calsum(int, float, int) // function prototype declaration int main() { int a,c; float b; cout<<"Enter any three numbers"<<endll cin>>a>>b>>c; cout<<"Sum of three numbers is "<<calsum(a,b,c)<<endl; // function call done diff return 0; } // The function calsum float calsum(int x, float y, int z) // function header { float d; d = x+y+z; return(d); } Example 5 #include<iostream.h> void calsum(int, float, int) // function prototype declaration int main() { int a,c; float b; cout<<"Enter any three numbers"<<endll cin>>a>>b>>c; calsum(); return 0; } // The function calsum void calsum(int x, float y, int z) // function header { float d; d = x+y+z; cout<<" The sum is "<<d; // no return required } Function concepts studied Function definition return data type function name( parameters) Function prototype declaration return data type function name ( data type list) Calling a function message(); sum=calsum(a,b,c); cout<<" sum of three numbers is "<<calsum(a,b,c); Returning a value to the function Not applicable for void data type. Applicable for all other datatypes ...
View Full Document

This document was uploaded on 07/06/2011.

Ask a homework question - tutors are online