FUNCTION Functions are building blocks of the programs. They

advertisement
FUNCTION
Functions are building blocks of the programs. They make the programs more modular and easy
to read and manage. All C++ programs must contain the function main( ). The execution of the
program starts from the function main( ). A C++ program can contain any number of functions
according to the needs. The general form of the function is: return_type function_name(parameter list)
{
body of the function
}
The function of consists of two parts function header and function body. The function header is:return_type function_name(parameter list)
The return_type specifies the type of the data the function returns. The return_type can be void
which means function does not return any data type. The function_name is the name of the
function. The name of the function should begin with the alphabet or underscore. The parameter
list consists of variables separated with comma along with their data types. The parameter list
could be empty which means the function do not contain any parameters. The parameter list
should contain both data type and name of the variable. For example,
int factorial(int n, float j)
is the function header of the function factorial. The return type is of integer which means
function should return data of type integer. The parameter list contains two variables n and j of
type integer and float respectively. The body of the function performs the computations.
Function Arguments
The information is transferred to the function by the means of arguments when a call to a
function is made. Arguments contain the actual value which is to be passed to the function when
it is called. The sequence of the arguments in the call of the function should be same as the
sequence of the parameters in the parameter list of the declaration of the function. The data types
of the arguments should correspond with the data types of the parameters. When a function call
is made arguments replace the parameters of the function.
Function not return value and using local variable
Ex1. Write a program to display the average of two numbers.
#include<iostream>
using namespace std;
void Calculate(float,float); //function declaration/prototype
void main(){
float num1,num2; //local variable
cout<<”input number 1 : “;
cin>>num1
cout<<”input number2 : “;
cin>>num2
Calculate(num1,num2); //calling function Calculate with two arguments
}
void Calculate(float n1, float n2){
//function Calculate has two parameters and not return any value
float average;
average = (n1+n2)/2;
cout<<”average is “<<average;
}
Function not return value and using global variable
Ex1. Write a program to display the average of two numbers.
#include<iostream>
using namespace std;
void Calculate(); //function declaration/prototype
float num1,num2; //global variable
void main(){
cout<<”input number 1 : “;
cin>>num1
cout<<”input number2 : “;
cin>>num2
Calculate( ); //calling function Calculate without arguments
}
void Calculate( ){
//function Calculate has no parameters and not return any value
float average;
average = (num1+num2)/2;
cout<<”average is “<<average;
}
Function return value
Ex1. Write a program to display the average of two numbers.
#include<iostream>
using namespace std;
float Calculate(float, float); //function declaration/prototype
int main(){
float num1,num2; //local variable
cout<<”input number 1 : “;
cin>>num1
cout<<”input number2 : “;
cin>>num2
cout<<”Average is “<<Calculate(num1,num2 ); //calling function Calculate and
// display the return value
return 0;
}
float Calculate(float n1, float2 ){
float average;
//function Calculate has two parameters and return float value
average = (n1+n2)/2;
return average;
}
OR
#include<iostream>
using namespace std;
float Calculate(float, float); //function declaration/prototype
int main(){
float num1,num2,average = 0; //local variable
cout<<”input number 1 : “;
cin>>num1
cout<<”input number2 : “;
cin>>num2
average=Calculate(num1,num2 ); //calling function Calculate and the return value is assigned
//to average variable
cout<<”Average is “<<average;
return 0;
}
float Calculate(float n1, float n2 ){
//function Calculate has two parameters and return float value
return (n1+n2)/2;
}
Exercise
1. Write a program that will display the largest number from two numbers input by a user.
#include<iostream>
using namespace std;
int Compare(int, int); //function declaration/prototype
int main(){
int num1,num2; //local variable
cout<<”input number 1 : “;
cin>>num1
cout<<”input number2 : “;
cin>>num2
cout<<”The largest number is “<<Compare(num1,num2 ); //calling function Compare and
// display the return value
return 0;
}
int Compare(int n1, int n2 ){
if (n1>n2)
return n1;
else
}
return n2;
//function Compare has two parameters and return integer value
2. Write the prototypes of the function including the parameter (formal argument) declarations:
a)
A function called Purchase generates and return a double value.
double Purchase( );
b)
A function called Payment accepts two integer arguments, one char argument and
returns a floating-point result.
float Payment(int,int,char);
c)
A function called changeCase accepts two characters and returns another character.
Char changeCase(char,char);
d)
A function called rectangle accepts a character and returns an integer.
int rectangle (char);
e)
A function called DisplayResult accepts two integer values and one floating –point
values .
void DisplayResult(int,int,float);
Download