Function - Subparts of A Program, To Easily Debug The Program. 2 Kinds of Function
Function - Subparts of A Program, To Easily Debug The Program. 2 Kinds of Function
2 Kinds of Function
1. Predefined function
2. User/Programmer defined function
2. Calling function
Syntax: functionName(arguments);
3. Function definition
Syntax: returnDataType functionName(parameters) function header
{
statements; function body
return statement;
}
Syntax: Passed-by-value;
Example:
Syntax: Passed-by-reference;
Example:
void SumPro (int n1, int n2, int& sum, int& product)
Return Statement
- Causes the value to be returned to the calling function.
- Should use data type specified in function header;
- Calling function must assign the called function to a variable of the same data type
as the called function’s return type.
Functions
1. Passed many values, return many values. – pass-by-reference
2. Passed many values, return single value. – pass-by-value / pass-by-reference
3. Passed single value, return single value. – pass-by-value / pass-by-reference
4. Passed multiple values, no return value – pass-by-value / pass-by-reference
5. Passed single value, no return value – pass-by-value / pass-by-reference
6. No passed value, no return value –
Sample Program Sample Program
//Passed-by-reference //Passed-by-value
//Passed multiple values, return multiple //Passed multiple values, return single value
values
#include <iostream>
#include <iostream> using namespace std;
using namespace std;
int sum(int n1, int n2);
void sumprod(int n1, int n2, int& sum, int&
product); int main( )
{
int main( ) int num1, num2, s ;
{ cout<<”Enter first value: “;
int num1, num2, s, p ; cin>>num1;
cout<<”Enter first value: “; cout<<”Enter second value: “;
cin>>num1; cin>>num2;
cout<<”Enter second value: “; s = sum(num1, num2);
cin>>num2; cout<<”Sum is : “<<s<<endl;
sum(num1, num2, s, p); return 0;
cout<<”Sum is : “<<s<<endl; }
cout<<”Product is “<<p<<endl;
return 0; int sum(int n1, int n2)
} {
int total;
void sumprod(int n1, int n2, int& sum, int& total = n1 + n2;
product) return total;
{ }
sum = n1 + n2;
product = n1*n2;
return ;
}
Sample Program 1. return total;
}
//Passed-by-reference
//Passed multiple values, return multiple
values
#include <iostream>
using namespace std;
int main( )
{
int num1, num2, s, p ;
cout<<”Enter first value: “;
cin>>num1;
cout<<”Enter second value: “;
cin>>num2;
sum(num1, num2, s, p);
cout<<”Sum is : “<<s<<endl;
cout<<”Product is “<<p<<endl;
return 0;
}
Sample Program 2.
//Passed-by-value
//Passed multiple values, return single value
#include <iostream>
using namespace std;
int main( )
{
int num1, num2, s ;
cout<<”Enter first value: “;
cin>>num1;
cout<<”Enter second value: “;
cin>>num2;
s = sum(num1, num2);
cout<<”Sum is : “<<s<<endl;
return 0;
}
//Passed-by-value
//Passed single value, return single value
//This program will determine if the number is
// odd or even
#include <iostream>
using namespace std;
int main( )
{
int num, val;
cout<<”Enter a number “;
cin>>num;
val=OddEven(num);
if (val)
cout<<”The number is Odd “<<endl;
else
cout<<”The number is Even “<<endl;
return 0;
}
int OddEven(int n)
{
int rem;
rem = n%2;
return rem;
}
Sample Program 3.
Sample Program 6
//Passed-by-reference
//Passed multiple values, return single value //Passed-by-value
//Passed multiple values, no return value
#include <iostream>
using namespace std; #include <iostream>
using namespace std;
void sum(int n1, int n2, int& total);
void Sum(int n1, int n2);
int main( )
{ int main( )
int num1, num2, s ; {
cout<<”Enter first value: “; int num1, num2;
cin>>num1; cout<<”Enter first value: “;
cout<<”Enter second value: “; cin>>num1;
cin>>num2; cout<<”Enter second value: “;
sum(num1, num2, s); cin>>num2;
cout<<”Sum is : “<<s<<endl; sum(num1, num2);
return 0; return 0;
} }
void sum(int n1, int n2, int& total) void Sum(int n1, int n2)
{ {
total = n1 + n2; int total;
return; total = n1+n2;
} cout<<”The sum is “<<total<<endl;
return;
}
Sample Program 5. }
Sample Program 8
//Passed-by-reference
//Passed single value, return single value //Passed-by-value
//This program will determine if the number //Passed single value, no return value
is
// odd or even #include <iostream>
using namespace std;
#include <iostream>
using namespace std; void OddEven(int n);
//Passed-by-reference
//Passed single value, no return value
#include <iostream>
using namespace std;
int main( )
{
int num;
cout<<”Enter a number: “;
cin>>num;
OddEven(num);
return 0;
}
void OddEven(int& n)
{
int rem;
if (rem = n%2)
cout<<”The number is Odd “<<endl;
else
cout<<”The number is Even “<<endl;
return;
}
Scope of Variable
Sample Program:
firstnum = 10;
snum = 20;
valfun( );
return 0;
}
int num;
void valfun()
{
int secnum;
secnum = 30;
num=45;