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 ► #include void message(); // function prototype declaration void message(); // function prototype declaration int main() int main() { message(); // calling the function message message(); // calling the function message cout<<“I am in main() function”<<endl; cout<<“I am in main() function”<<endl; return 0; return 0; } // the additional function message // the additional function message void message() // the function header line void message() // the function header line { cout<<“I am in function message”<<endl; // body of function cout<<“I am in function message”<<endl; // body of function } output: output: I am in function message I am in function message I am in main() function I am in main() function
Example 2 ► #include ► #include int calsum(int, int, int) // function prototype declaration int calsum(int, int, int) // function prototype declaration int main() int main() { int a,b,c,sum; int a,b,c,sum; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here sum=calsum(a,b,c); // function call done here return 0; return 0; } // The function calsum // The function calsum int calsum(int x, int y, int z) // function header int calsum(int x, int y, int z) // function header { int d; int d; d = x+y+z; d = x+y+z; return(d); return(d); }
Example 3 ► #include ► #include float calsum(int, float, int) // function prototype declaration float calsum(int, float, int) // function prototype declaration int main() int main() { int a,c; int a,c; float b,sum; float b,sum; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here sum=calsum(a,b,c); // function call done here return 0; return 0; } // The function calsum // The function calsum float calsum(int x, float y, int z) // function header float calsum(int x, float y, int z) // function header { float d; float d; d = x+y+z; d = x+y+z; return(d); return(d); }
Example 4 ► #include ► #include float calsum(int, float, int) // function prototype declaration float calsum(int, float, int) // function prototype declaration int main() int main() { int a,c; int a,c; float b; float b; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; cout<<“Sum of three numbers is “<<calsum(a,b,c)<<endl; // function call done diff cout<<“Sum of three numbers is “<<calsum(a,b,c)<<endl; // function call done diff return 0; return 0; } // The function calsum // The function calsum float calsum(int x, float y, int z) // function header float calsum(int x, float y, int z) // function header { float d; float d; d = x+y+z; d = x+y+z; return(d); return(d); }
Example 5 ► #include ► #include void calsum(int, float, int) // function prototype declaration void calsum(int, float, int) // function prototype declaration int main() int main() { int a,c; int a,c; float b; float b; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; calsum(); calsum(); return 0; return 0; } // The function calsum // The function calsum void calsum(int x, float y, int z) // function header void calsum(int x, float y, int z) // function header { float d; float d; d = x+y+z; d = x+y+z; cout<<“ The sum is “<<d; // no return required cout<<“ The sum is “<<d; // no return required }
Function concepts studied ► Function definition return data type function name( parameters) return data type function name( parameters) ► Function prototype declaration return data type function name ( data type list) return data type function name ( data type list) ► Calling a function ► message(); sum=calsum(a,b,c); sum=calsum(a,b,c); cout<<“ sum of three numbers is “<<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 Not applicable for void data type. Applicable for all other datatypes