You are on page 1of 10

Function – subparts of a program, to easily debug the program.

2 Kinds of Function
1. Predefined function
2. User/Programmer defined function

• A function is called by using function’s name and passing data as arguments

3 Parts of User/Programmer defined function


1. Function prototype
Syntax: returnDataType functionName(parameters);

2. Calling function
Syntax: functionName(arguments);

3. Function definition
Syntax: returnDataType functionName(parameters)  function header
{
statements; function body
return statement;
}

2 Kinds of User/Programmer defined function

1. Passed-by-value : when a function receive only a copy of the data passed in as


the argument.
- The function cannot alter the actual variable containing the
data.
- A function can directly return only one value, although it can
receive many values.
- Value arguments can be a variables and constants.

2. Passed-by-reference : A called function is given the address of a variable as an


argument.
- Allows the called function to directly alter the variable’s
value.
- Ability to change actual variable provides a means to “return
multiple values.”
- Reference arguments must be variables, not constants.
- Function may contain a mix of by value and by reference
arguments
- Function call does not indicate that reference arguments are
used in the called function.
- Reference parameters should be used only in restricted
situations that require multiple return.
Function Prototype

Syntax: Passed-by-value;

returnDataType functionName(dataType varName1, dataType varName2,.. );

Example:

int Sum (int n1, int n2);

Syntax: Passed-by-reference;

void functionName(dataType& varName1, dataType& varName2,.. );

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;

void sumprod(int n1, int n2, int& sum, int&


product);

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;
}

void sumprod(int n1, int n2, int& sum, int&


product)
{
sum = n1 + n2;
product = n1*n2;
return ;
}

Sample Program 2.

//Passed-by-value
//Passed multiple values, return single value

#include <iostream>
using namespace std;

int sum(int n1, int n2);

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;
}

int sum(int n1, int n2)


{
int total;
total = n1 + n2;
Sample Program 4

//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 OddEven(int n);

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);

void OddEven(int n, int& rem); int main( )


{
int main( ) int num;
{ cout<<”Enter a number: “;
int num,val ; cin>>num;
cout<<"Enter a number: "; OddEven(num);
cin>>num; return 0;
OddEven(num,val); }
if (val)
cout<<"The number is Odd "<<endl; void OddEven(int n)
else {
cout<<"The number is Even "<<endl; int rem;
return 0; if (rem = n%2)
} cout<<”The number is Odd “<<endl;
else
void OddEven(int n, int& rem) cout<<”The number is Even “<<endl;
{
rem=n%2; return;
return; }
}
Sample Program 10

Sample Program 7 //Function


//No Passed value, no return value
//Passed-by-refernce
//Passed multiple values, no return value #include <iostream>
#include <string>
#include <iostream> using namespace std;
using namespace std;
void Message( );
void Sum(int& n1, int& n2);
int main( )
int main( ) {
{ string name;
int num1, num2; cout<<”Enter your name: “;
cout<<”Enter first value: “; cin>>name;
cin>>num1; Message( );
cout<<”Enter second value: “; cout<<name<<endl;
cin>>num2; return 0;
Sum(num1, num2); }
return 0;
} void Message( )
{
void Sum(int& n1, int& n2) system (“cls”);
{ cout<<”Hi ! Welcome to C++ “;
int total; return;
total = n1+n2; }
cout<<”The sum is “<<total<<endl;
return;
Sample Program 9

//Passed-by-reference
//Passed single value, no return value

#include <iostream>
using namespace std;

void OddEven(int& n);

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

Local variables : variables declared within a function.


They can be used only within the function in which they are declared.

Global variables : variables declared outside any function.


They can be used by functions that occur after their declaration.

Sample Program:

#include <iostream> Data Dictionary


using namespace std;
varName dataType scope function
int firstnum;
firstnum int global main,valfun, difference
void valfun( ); snum int local main
void difference(int n1, int n2); secnum int local valfun
num int global valfun, difference
int main() diff int local difference
{ n1 int local difference
int snum; n2 int local difference

firstnum = 10;
snum = 20;

cout<<”From main() : firstnum = “<<firstnum<<endl;


cout<<”From main() : snum = “<<secnum<<endl;

valfun( );

cout<<”\nFrom main( ) again: firstnum = “ <<firstnum<<endl;


cout<<”From main() again: snum = “ <<secnum<<endl;
difference(firstnum,snum);

return 0;
}
int num;
void valfun()
{
int secnum;

secnum = 30;
num=45;

cout<<”\nFrom valfun() : firstnum = “<<firstnum<<endl;


cout<<”From valfun() : secnum = “<<secnum<<endl;
cout<<”Num is = “<<num<<endl;

firstnum=40; Screen Output

return; From main() : firstnum = 10


} From main() : snum = 20

void difference( int n1, int n2) From valfun() : firstnum = 10


{ From valfun() : secnum = 30
int diff;
num=12; From main() again : firstnum = 40
diff = n1-n2; From main() again : snum = 20
cout<<”\nDifference is “<<diff<<endl;
return; Difference is 20
}

You might also like