You are on page 1of 36

C++ Functions

Agenda
• What is a function? • Sharing data among
• Types of C++ functions: functions through
function parameters
• Standard functions
• Value parameters
• User-defined functions
• Reference parameters
• Declaring and
Implementing C++ • Scope of variables
functions • Local Variables
• Global variable

2
Functions and subprograms
• The Top-down design approach is based on dividing the
main problem into smaller tasks which may be divided
into simpler tasks, then implementing each simple task
by a subprogram or a function
• A C++ function or a subprogram is simply a chunk of C++
code that has
• A descriptive function name, e.g.
• computeTaxes to compute the taxes for an employee
• A returning value
• The computeTaxes function may return with a double number
representing the amount of taxes

3
Advantages of Functions
• Functions separate the concept (what is done) from the
implementation (how it is done).
• Functions make programs easier to understand.
• Functions can be called several times in the same program,
allowing the code to be reused.

4
C++ Standard Functions
• C++ language is shipped with a lot of functions
which are known as standard functions
• These standard functions are groups in different
libraries which can be included in the C++
program, e.g.
• Math functions are declared in <math.h> library
• Character-manipulation functions are declared in
<ctype.h> library
• C++ is shipped with more than 100 standard libraries,
some of them are very popular such as <iostream.h>
and <stdlib.h>, others are very specific to certain
hardware platform, e.g. <limits.h> and <largeInt.h>
5
Example of Using
Standard C++ Math Functions
#include <iostream.h>
#include <math.h>
void main()
{
// Getting a double value
double x;
cout << "Please enter a real number: ";
cin >> x;
// Compute the ceiling and the floor of the real number
cout << "ceiling= " << ceil(x) << endl;
cout << "floor= " << floor(x) << endl;
} 6
User-Defined C++ Functions
• C++ provides its users with a way to define their
own functions (or user-defined function)
• A C++ function consists of two parts
• The function header, and
• The function body
• The function header has the following syntax
<return value> <name> (<parameter list>)
• The function body is enclosed between { }

7
Defining a Function
return_type function_name( parameter list )
{
body of the function
}

8
Example of User-defined
C++ Function
Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and return
the biggest of both

9
Example of User-defined
C++ Function
// function returning the max between two
numbers
int max(int num1, int num2)
{ // local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
10
}
#include <iostream>
using namespace std;
int max(int num1, int num2);
int main ()
{
int a = 100; int b = 200; int ret;
ret = max(a, b); // calling a function to get max value.
cout << "Max value is : " << ret << endl;
return 0;
}
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else result = num2; 11
return result;
}
Function Definition
A function definition has the following syntax:
<type> <function name>(<parameter list>){
<local declarations>
<sequence of statements>
}
For example: Definition of a function that computes the absolute
value of an integer:

int absolute(int x){


if (x >= 0) return x;
else return -x;
}
Function Prototype
• The function prototype declares the input and output parameters of
the function.

• The function prototype has the following syntax:


<type> <function name>(<type list>);

• Example: A function that returns the absolute value of an integer is:


int absolute(int);
Function Call

• A function call has the following syntax:


<function name>(<argument list>)

Example: int distance = absolute(-5);


• The result of a function call is a value of type <type>
Calling a Function
• To call a function, you simply need to pass the required
parameters along with function name, and if function returns
a value, then you can store returned value. For example −

15
Absolute Value
#include <iostream>
using namespace std;
int absolute (int);// function prototype for absolute()
int main(){
int num, answer;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num!=0){
answer = absolute(num);
cout << "The absolute value of " << num
<< " is: " << answer << endl;
cin >> num; }
return 0; }
// Define a function to take absolute value of an
integer
int absolute(int x){
if (x >= 0) return x;
else return -x; }
Function Definition
• The function definition can be placed anywhere in the program after
the function prototypes.

• If a function definition is placed in front of main(), there is no


need to include its function prototype.
#include <iostream>
using namespace std;
int absolute(int x){
if (x >= 0) return x;
else return -x; }
int main(){
int num, answer;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num!=0){
answer = absolute(num);
cout<< "The absolute value of "
cout<< num << " is: " << answer<<endl;
cin >> num;
}
return 0;
}
19
20
Function of three parameters
#include <iostream>
using namespace std;
double total_second(int, double ,double );
int main(){
cout << total_second(1,1.5, 2) << endl;
return 0;
}
double total_second( int hour, double minutes,
double second)
{
return hour*3600 + minutes * 60 + second;
}
Exercise / Assignment
• Calculate the Area of a Circle
with a Function

22
II. Function Arguments
Call Type & Description
• Call by value – copies the actual value of an
argument into the formal parameter of the
function. In this case, changes made to the
parameter inside the function have no effect on
the argument.
• Call by reference – copies the reference of an argument into
the formal parameter. Inside the function, the reference is used to
access the actual argument used in the call

23
Example of Using Value Parameters
and Global Variables
#include <iostream.h> x 0
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4; void main()
fun(x/2+1); {
1 x = 4;
cout << x << endl; fun(x/2+1); 24
} cout << x << endl;
}
Example of Using Value Parameters
and Global Variables
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5; void fun(int x )
} {
void main() 3 cout << x << endl;
x=x+5;
{ }
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
3
} x = 4;
25
2 fun(x/2+1);
cout << x << endl;
}
Example of Using Value Parameters
and Global Variables
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5; void fun(int x 8
3 )
} {
void main() cout << x << endl;
4 x=x+5;
{ }
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
} x = 4;
26
2 fun(x/2+1);
cout << x << endl;
}
Example of Using Value Parameters
and Global Variables
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5; void fun(int x 3
8 )
} {
void main() cout << x << endl;
x=x+5;
{ 5 }
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
} x = 4;
27
2 fun(x/2+1);
cout << x << endl;
}
Example of Using Value Parameters
and Global Variables
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4;
fun(x/2+1); void main()
cout << x << endl; {
} x = 4;
28
fun(x/2+1);
6 cout << x << endl;
}
Example of Using Value Parameters
and Global Variables
#include <iostream.h> x 4
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
void main()
{
x = 4; void main()
fun(x/2+1); {
x = 4;
cout << x << endl; fun(x/2+1); 29
} cout << x << endl;
7 }
Reference Parameters
• As we saw in the last example, any changes in
the value parameters don’t affect the original
function arguments
• Sometimes, we want to change the values of the
original function arguments or return with more
than one value from the function, in this case we
use reference parameters
• A reference parameter is just another name to the
original argument variable
• We define a reference parameter by adding the & in
front of the parameter name, e.g.
double update (double & x);
30
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local
variable void main()
{
fun(x); 1 int x = 4; ?
4 x
cout << x << endl; fun(x);
cout << x << endl; 31
}
}
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
void fun( int & y )
y=y+5; {
} 3 cout<<y<<endl;
void main() y=y+5;
}
{
int x = 4; // Local variable void main()
fun(x); {
cout << x << endl; int x = 4; ?
4 x
2 fun(x);
} cout << x << endl; 32
}
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
void fun( int & y )
y=y+5; {
} cout<<y<<endl;
void main() 4 y=y+5;
9
}
{
int x = 4; // Local variable void main()
fun(x); {
cout << x << endl; int x = 4; ?
4 x
2 fun(x);
} cout << x << endl; 33
}
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
void fun( int & y )
y=y+5; {
} cout<<y<<endl;
void main() y=y+5;
5 }
{
int x = 4; // Local variable void main()
fun(x); {
cout << x << endl; int x = 4; ?
9 x
2 fun(x);
} cout << x << endl; 34
}
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable void main()
fun(x); {
cout << x << endl; int x = 4; ?
9 x
fun(x);
} cout << x << endl; 35
6
}
Example of Reference Parameters
#include <iostream.h>
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
void main()
{
int x = 4; // Local variable void main()
fun(x); {
cout << x << endl; int x = 4; ?
9 x
fun(x);
} cout << x << endl; 36
7 }

You might also like