Sxith Laboratory Session - Functions
Sxith Laboratory Session - Functions
Sxith Laboratory Session - Functions
Agenda
• What is a function?
• Sharing data among functions
• Types of C++ functions: through function parameters
• Standard functions • Value parameters
• User-defined functions • Reference parameters
• C++ function structure • Const reference parameters
• Function signature • Scope of variables
• Function body • Local Variables
• Declaring and • Global variable
Implementing C++
functions
2
Functions and subprograms
• A function is a group of statements that together perform a task.
• Every C++ program has at least one function, which is main(), and all
the most trivial programs can define additional functions.
• You can divide up your code into separate functions.
• How you divide up your code among different functions is up to you,
but logically the division usually is such that each function performs a
specific task.
3
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>
4
User-Defined C++ Functions
• Although C++ is shipped with a lot of standard functions, these
functions are not enough for all users, therefore, C++ provides its
users with a way to define their own functions (or user-defined
function)
• For example, the <math.h> library does not include a standard
function that allows users to round a real number to the ith digits,
therefore, we must declare and implement this function ourselves
5
How to define a C++ Function?
• Generally speaking, we define a C++ function in two steps (preferably
but not mandatory)
• Step #1 – declare the function signature in either a header file (.h file) or
before the main function of the program
• Step #2 – Implement the function in either an implementation file (.cpp) or
after the main function
6
What is The Syntactic Structure of a C++ 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 simply a C++ code enclosed between { }
7
Example of User-defined
C++ Function
8
Example of User-defined
C++ Function
Function
header
9
Example of User-defined
C++ Function
Function Function
header body
10
Function Signature
• The function signature is actually similar to the function header
except in two aspects:
• The parameters’ names may not be specified in the function signature
• The function signature must be ended by a semicolon
Unnamed Semicolon
• Example Parameter ;
double computeTaxes(double) ;
11
First Function Example
Functions
• This program is divided in two functions: addition and main.
• Remember that no matter the order in which they are defined, a C++
program always starts by calling main.
• In fact, main is the only function called automatically, and the code in
any other function is only executed if its function is called from main
(directly or indirectly).
Functions
• In the example above, main begins by declaring the variable z of
type int, and right after that, it performs the first function call: it
calls addition.
• The call to a function follows a structure very similar to its
declaration.
• In the example above, the call to addition can be compared to its
definition just a few lines earlier:
Functions
• The parameters in the function declaration have a clear
correspondence to the arguments passed in the function call.
• The call passes two values, 5 and 3, to the function; these correspond
to the parameters a and b, declared for function addition.
• At the point at which the function is called from within main, the
control is passed to function addition: here, execution of main is
stopped, and will only resume once the addition function ends.
Functions
• At the moment of the function call, the value of both arguments (5
and 3) are copied to the local variables int a and int b within the
function.
• Then, inside addition, another local variable is declared (int r), and
by means of the expression r=a+b, the result of a plus b is assigned
to r; which, for this case, where a is 5 and b is 3, means that 8 is
assigned to r.
Functions
• The final statement within the function: return r;
• Ends function addition, and returns the control back to the point
where the function was called; in this case: to function main.
• At this precise moment, the program resumes its course on main
returning exactly at the same point at which it was interrupted by the
call to addition.
• But additionally, because addition has a return type, the call is
evaluated as having a value, and this value is the value specified in the
return statement that ended addition: in this particular case, the
value of the local variable r, which at the moment of the return
statement had a value of 8.
Functions