Documente Academic
Documente Profesional
Documente Cultură
2
Definition
A function is a group of statements that is executed
when it is called from some point of the program
3
Advantages of Function
Divide complicated programs into manageable pieces.
Software reusability
using the existing functions as building blocks to create
new programs.
Enhance the program’s readability
It reduces the complexity of the function main.
4
Types of Function
5
Built-in Function
The C++ standard library provides a rich
collection of functions for performing
common mathematical calculations,
string manipulations, character
manipulations, input/output and many
other useful operations.
6
Built-in Function
7
Built-in Function
Example
#include <iostream.h>
#include <math.h> // The <math.h> is needed for the sqrt() function
int main()
{
double a = 2.0, b = 4.0, c = 7.0, d;
8
User-defined Function
The programmer can write functions to
define specific tasks that may be used at
many points in a program.
Two categories:
1. Value-returning functions
Functions that have a return type
2. Void functions
Functions that do not have a return type
9
User-defined Function
Example of value-returning function:
#include <iostream.h>
int square (int); // function prototype
int main()
{
for (int x = 1; x <= 10; x++)
cout << square(x) << " ";
11
Function Header
12
Function Header
where:
functionType : is the data type of the result returned to
the caller. The functionType void indicates that a
function does not return a value. An unspecified
functionType is assumed by the compiler to be as int.
13
Function Header
formal parameter list (as many as needed): Each
parameter consists of a data type followed by an
identifier, like any regular variable declaration (for
example: int a) and which acts within the function
as a regular local variable.
- it specifies the parameters received by the
function when it is called.
- if a function does not receive any values,
formal parameter list is void.
- The different parameters are separated by
commas.
14
Function Header
Example:
// function example
#include <iostream.h>
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0; 15
}
Function Body
Start immediately after the closing parentheses of the
function header, must be enclosed by curly braces.
Contain one or more statements.
16
Function Body
// function example
#include <iostream.h>
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
} 17
Local Variable
18
Local Variable
// function example
Question: #include <iostream.h>
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
} 19
Return Statement
There are three ways to return control from a called
function to the point at which a function was invoked.
If the function does not return a result, control is
returned simply when the function-ending right brace
is reached or by executing the statement
return;
20
Return Statement
If a function does return a result, the statement
return expression;
Return the value of expression to the caller.
21
Parameter Declaration List
CLO1,CLO2 &CLO 3
Function Prototypes
24
Function Prototypes
// function example
#include <iostream.h>
26
Function Calls
27
Function Calls
28
Function Calls (By Value)
#include<iostream.h>
float Add(float No1, float No2)
{
float total;
total=No1+No2;
No1=4.4;
No2=5.5;
return (total);
}
int main()
{
float a,b;
a = 2.2;
b = 3.3;
cout<<"Addition= "<<Add(a, b)<<endl;
cout<<"value for variable a in function main is : "<<a<<endl;
cout<<"value for variable b in function main is : "<<b;
return 0;
} 29
Function Calls (output)
30
Function Calls
31
Function Calls (By
Reference)
Reference parameter
#include<iostream.h>
float Add(float &No1, float &No2)
{
float total;
total=No1+No2;
No1=4.4;
No2=5.5;
return (total);
}
int main()
{
float a,b;
a = 2.2;
b = 3.3;
cout<<"Addition= "<<Add(a, b)<<endl;
cout<<"value for variable a in function main is : "<<a<<endl;
cout<<"value for variable b in function main is : "<<b;
return 0;
} 32
Function Calls (output)
33
Function Calls
// passing parameters by reference
#include <iostream.h> Reference parameter
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
34
Exercise
Write a program that takes a course score (a value between 0 and 100) and
determines a student’s course grade. This program has three functions:
main, getScore, and printGrade, as follows:
1. main
- get the course score
- print the course grade
2. getScore
- prompt the user for the input
- get the input
- print the course code
3. printGrade
- calculate the course grade
- print the course grade
Concept of Recursion
The process of solving a problem by reducing it to
smaller versions of itself is called recursion.
36
Concept of Recursion
Recursivity is the property that functions have to be
called by themselves.
It is useful for many tasks, like sorting or calculate the
factorial of numbers.
For example, to obtain the factorial of a number (n!)
the mathematical formula would be:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
more concretely, 5! (factorial of 5) would be:
5! = 5 * 4 * 3 * 2 * 1 = 120
37
Concept of Recursion
// factorial calculator
int main ()
#include <iostream.h> {
long factorial (long a) long number;
{ cout << "Please type a number: ";
if (a > 1) cin >> number;
return (a * factorial (a-1)); cout << number << "! = " << factorial
else (number);
return (1); return 0;
} }
38