Sunteți pe pagina 1din 38

Course Learning Outcomes

 Apply program structure and debugging process in


C++ programming language accordingly.
 Design programs using appropriate control
structures, arrays, structures, functions and pointers.
 Solve problem using C++ programming language
environment with proper coding style guidelines and
take in the security issues into consideration.

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

 Two types of function:


 Built-in / predefined function
 User-defined 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

Function Description Example Answer


sqrt(n) square root of n sqrt(4) 2

abs(n) absolute value of n abs(-7) 7

pow(a,b) a raised to power pow(5,2) 25


b(ab)

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;

d = sqrt ( a + b * c); //The sqrt() function is part of C++’s math library.


cout << “The result is ” << d;
return 0;
}

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

cout << endl;


return 0;
}
//function definition
int square (int y)
{
return y * y;
} 10
Components of Function
 Components of function
(a) Function header
(b) Function body
(c) Local variable
(d) Return statement
(e) Parameter declaration list
(f) Function prototypes

11
Function Header

 Function Header consists of function


type, function name and formal
parameter list.
 Basic syntax:
functionType functionName (formal parameter list)
{
statements
}

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.

functionName : is any valid identifier

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 addition (int a, int b) Function Header


{
int r;
r = a + b;
return (r);
}

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 addition (int a, int b)


{
int r; Function body
r = a + b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
} 17
Local Variable

 All variables declared in function definitions are


local variables – they are known only in the
function in which they are defined.
 Most functions have a list of parameters that provide
the means for communicating information between
functions.
 A function’s parameters are also local variables of
that function.

18
Local Variable
// function example
Question: #include <iostream.h>

Identify local variables from int addition (int a, int b)


the program given. {
int r;
r = a + b;
return (r);
}

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

• 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
• The different parameters are separated by
commas.
22
WEEK 14

CLO1,CLO2 &CLO 3
Function Prototypes

 tells the compiler the type of data returned by the


function, the number of parameters the function
expects to receive, the types of the parameters and the
order in which these parameters are expected.
 The compiler uses function prototypes to validate
function calls.
 It is not required if the definition of the function
appears before the main function’s use in the program

24
Function Prototypes
// function example
#include <iostream.h>

int addition (int a, int b); //function prototype

int main () Actual parameter/


{ Argument
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
} Formal parameter
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
25
}
Function Call
• A function call in a program causes the body
of the called function to execute.
• The syntax to call a function is:
functionName (actual parameter list)

• The number of actual parameters, together


with their data types, must match with the
formal parameter in the order given.

26
Function Calls

 There are two types of function calls:


(a) call by value
(b) call by reference

27
Function Calls

(a) call by value


- when arguments are passed by value,
a copy of the argument’s value is
made and passed to the called
function.
- changes to the copy do not affect an
original variable’s value in the caller

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

(b) call by reference


- when an argument is passed by
reference, the caller actually allows the
called function to modify the original
variable’s value.

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.

Recursion is a very powerful way to solve certain


problems for which the solution would otherwise be very
complicated.

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

S-ar putea să vă placă și