CPP Lecture6
CPP Lecture6
Functions
A function groups a number of program statements into a unit and gives it
a name. This unit can then be invoked from other parts of the program as
shown in the figure below. Two reasons to use functions: 1) to aid in the
conceptual organization of a program. (However, object-oriented
programming is more powerful). 2) to reduce program size.
#include <filename.h>
function declaration;
void main ( )
{
...
...
call of function;
...
...
call of function;
...
...
}
function definition( )
{
// function body
...
...
}
Calling Function
The function can be called (or invoked, or executed) many times in the
program.
function-name(argument list) ;
Examples:
print();
fact = factorial(5);
cout<<average(90 , 70);
Executing the call statement causes the function to execute; that is,
control is transferred to the function, the statements in the function
definition are executed, and then control returns to the statement
following the function call.
Function Definition
The function definition contains the actual code for the function.
return-type function-name(argument list)
{
// function body
…
…
}
Examples:
void print()
{
for(int i=0;i<80;i++)
cout<<"*";
cout<<endl;
}
Example: Write a C++ program that prints a line of 25 stars using the
function starline().
#include <iostream.h>
void starline(); //function declaration (prototype)
void main()
{
starline(); //call of function
cout << "Student Name\tMark" << endl;
starline(); //call of function
cout<< "Ahmed\t\t80" << endl
<< "Ali\t\t90" << endl
<< "Hassan\t\t70" << endl
<< "Sara\t\t85" <<endl;
starline(); //call of function
}
Example: Write a C++ program that adds two integer numbers using the
function add().
#include<iostream.h>
int add(int , int); // function declaration
void main()
{
int x , y ,z;
cout<<"Enter two integer numbers: ";
cin >> x >> y;
z = add(x,y); // call of function
cout<<"Result= "<< z << endl;
}
int sqr(int x)
{
return (x*x);
}
Example: Write a C++ program that finds the maximum of two entered
numbers using the function max( ).
#include<iostream.h>
int max(int , int);
void main()
{
int x , y;
cout<<"Enter two integer numbers: ";
cin >> x >> y;
cout<<"The maximum number is " << max(x,y) <<endl;
}
long factorial(int a)
{
long fact = 1;
for(int i = a ; i > 1 ; i--)
fact *= i;
return fact;
}
Example: Write a C++ program that computes the shaded area in the
figure below using the function area().
#include<iostream.h>
float area(float , float);
void main()
{
float x1 = 10 , y1 = 20;
float x2 = 7 , y2 = 15;
cout<<"The shaded area is "
<< area(x1,y1) - area(x2,y2)
<< endl;
}
int issmall(char c)
{
if (c >= 'a' && c <= 'z')
return 1;
else
return 0;
}
Example: Write a C++ program that finds the max value in an integer
array a[10] using the function maxa().
#include<iostream.h>
int maxa(int b[10]);
void main()
{
int a[10];
cout<<"Enter ten integer values: ";
for(int i=0; i<10 ; i++)
cin >> a[i];
cout<<"The max value is "<< maxa(a) << endl;
}
Example: Write a C++ program that reads, sorts and prints an integer
array a[10] using three functions setarray(), sortarray(), and
putarray().
#include<iostream.h>
void setarray(int b[10]);
void sortarray(int b[10]);
void putarray(int b[10]);
void main()
{
int a[10];
setarray(a);
sortarray(a);
cout<<"Sorted array is ";
putarray(a);
}
Passing by Reference
• When we need the function returns more than one value, we can
pass arguments by reference to the function.
• A reference provides an alias—another name—for a variable (It’s
actually the memory address of the variable that is passed).
• An important advantage of passing by reference is that the function
can access the actual variables in the calling program. This
provides a mechanism for passing more than one value from the
function back to the calling program.
void sqr(int& x)
{
x = x * x;
}
Example: Write a C++ program that reads a real number, and then
separates this number into an integer and fractional part using the
function intfrac().
#include<iostream.h>
void intfrac(float, float& , float&);
void main()
{
float number , intpart , fracpart;
cout<<"Enter a real number: ";
cin >> number;
intfrac(number, intpart , fracpart);
cout<<"The integer part is "<< intpart << endl
<<"The fraction part is "<< fracpart << endl;
}
Homework:
1. Write a C++ program that determines whether an integer number is
positive, negative or zero using the function test().
2. Write a C++ program that computes the shaded area in the figure
below using the function carea( ). The radius
values are entered by the user.