Function- is a subprogram that acts on data and returns a value.

Function Call by Value Function Declaration – It is also called function prototype. It tells the compiler the name, return type, and type of the parameters of the function. The declaration of the function is called its prototype. Function Definition – tells the compiler how the function works. Syntax: Return_Type Function_Name(Argument Lists);

1.) Function with void as return type and without argument list Input Process Output User-Defined User-Defined User-Defined Function Function Function Example: void highest();

2.) Function with void as return type and with argument list Input Process Output main User-Defined User-Defined Function Function Function Example: void highest(int num1, int num2);

3.) Function with other data type as return type and without argument list Input Process Output User-Defined User-Defined main Function Function Function Example: int highest();

4.) Function with other data type as return type and with argument list Input Process Output main User-Defined main Function Function Function Example: int highest(int num1, int num2);

Sample Program: Create a C++ program that will input two numbers and output the highest number. Implement this using function.

cin>>num1>>num2. num2). cout<<"Input two numbers: ". int main() { highest(). num2. } void highest(int num1. int num2). return 0.h> void highest(). return 0. } Function Call Function Definition Function Declaration Function Declaration Function Call Function Definition . if(num1>num2) high=num1. int num2) { int high. cout<<"Input two numbers: ". } //This program will use function declaration 2 #include<iostream. if(num1>num2) high=num1. else high=num2.//This program will use function declaration 1 #include<iostream. high. int main() { int num1. num2. cin>>num1>>num2. cout<<"Highest number = "<<high<<endl.h> void highest(int num1. else high=num2. highest(num1. } void highest() { int num1. cout<<"Highest number = "<<high<<endl.

h> int highest(int num1. cout<<"Input two numbers: ". cout<<"Input two numbers: ". } //This program will use function declaration 4 #include<iostream. num2. } int highest(int num1. num2. if(num1>num2) high=num1. return (high). int main() { int high.//This program will use function declaration 3 #include<iostream. return 0. high=highest(num1. else high=num2. } Function Declaration Function Call Function Declaration Function Definition Function Call Function Definition . return 0. high=highest(). int num2) { int high. } int highest() { int num1. int num2). cin>>num1>>num2.h> int highest(). cout<<"Highest number = "<<high<<endl. cout<<"Highest number = "<<high<<endl. return (high). int main() { int high. else high=num2. high. cin>>num1>>num2. if(num1>num2) high=num1. num1. num2).

} int main() { int num1. byReference(num1. Note that in the following example.int &num2) { num1 = num1 + 1. cout<<"num2 before function = "<<num2<<endl. cout<< "num1 call by reference"<<num1<<endl. but this time output is in the main function. there is no Function declaration since the function definition is written before the main function. num2 = 6.h> void byReference(int &num1. num1 = 5. Example Number1: #include<iostream. process and output should be in user defined function. num2). cout<<"num2 after function call = "<<num2<<endl.Function Call by Reference Remember in the previous discussion that using function declaration void return type and with argument list that Input should be in main function. In such case. cout<<"num2 call by reference"<<num2<<endl. no need to write the function declaration. Process is in user defined function. num2 = num2 + 1. } . cout<< "num1 after function call = "<<num1<<endl. cout<< "num1 before function call = "<<num1<<endl. Using Function Call by Reference Input is still in main. num2. return 0.

Example Number2: /*This program will also prompt the user to input two numbers and output he highest number. } int main() { int num1. num2.int num2. int &high) { if(num1 > num2) high=num1. high=0. cout<<"Input two numbers: ". cin>>num1>>num2. Notice that it uses function declaration 2 but the Output is in the main function. return 0. else high=num2. cout<<"highest number = "<<high<<endl. } . high). byReference(num1. */ #include<iostream. num2.h> void byReference(int num1.

} int main() { int num.h> void oddeven(int num. } . cout<<"Input ten numbers: "<<endl. ctreven=0. third argument is also passed by reference*/ #include<iostream. cout<<"ctreven = "<<ctreven<<endl. ctrodd.ctr<=10. ctrodd=0. for(ctr=1. oddeven(num. } cout<<"ctrodd = "<<ctrodd<<endl. first argument is the number entered. ctreven). return 0. else ctrodd++. and with three argument lists. It uses function declaration with void as return type./* This is a program that will prompt the user to input ten numbers and output how many odd and even number were entered. int &ctreven) { if (num % 2 == 0) ctreven++. second argument is passed by reference. ctr.ctr++) { cin>>num.int &ctrodd.

}while(column<1 || row<1)./* This program will input values for rows and columns to be able to produce a multiplication table. for(int c=1. column.h> #include<ctype. }while (again == 'y'). "<<endl. cout<<"Press Y/y to try again. cout<<endl. int column) { int ans. again = tolower(again). column). Use function declaration with void as return type and two parameters row and column which is passed by value.c<=column. cout<<"\t"<<ans.r++) { cout<<r. for(int c=1. for(int r=1.h> void multtable(int row. return 0. if(column<1 || row<1) cout<<"Invalid input!"<<endl<<endl.*/ #include<iostream. } . cin>>again.c++) { ans=r*c.r<=row. cin>>row>>column. char again. } cout<<endl.c++) cout<<"\t"<<c. do{ do{ cout<<"Input rows and columns: "<<endl. } } int main() { int row. multtable(row.c<=column.h> #include<iomanip.

Sign up to vote on this title
UsefulNot useful