0% found this document useful (0 votes)
6 views

Lesson 5 - Function 1

This lesson introduces functions in C++ programming, emphasizing their role in structuring programs modularly. It outlines the syntax for defining functions, including the use of data types, names, parameters, and the function body. Additionally, it explains the use of 'void' for functions that do not return a value and the correct format for calling such functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lesson 5 - Function 1

This lesson introduces functions in C++ programming, emphasizing their role in structuring programs modularly. It outlines the syntax for defining functions, including the use of data types, names, parameters, and the function body. Additionally, it explains the use of 'void' for functions that do not return a value and the correct format for calling such functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ICT 301 – Object Oriented Programming

(Introduction to C++ Programming)

Lesson 05
FUNCTIONS (I)
• Using functions we can structure our programs in a more modular
way, accessing all the potential that structured programming can offer
to us in C++.

• A function is a group of statements that is executed when it is called


from some point of the program.

• The following is its format:


type name (parameter1, parameter2, …) {statements}
• Where:
• type is the data type specifier of the data returned by the function.

• name is the identifier by which it will be possible to call the function.

• parameters (as many as needed): Each consists of a data type specifier


followed by an identifier, like any regular variable declaration (for example: int
x) and which acts within the function as a regular local variable. They allow to
pass arguments to the function when it is called. The different parameters are
separated by commas.

• statements is the function’s body. It is a block of statements surrounded by


braces { }.
Function Example
Functions with no type. The use of void.
• If you remember the syntax of a function declaration:
type name (parameter1, parameter2, …) {statements}

• You will see that the declaration begins with a type, that is the type of the
function itself (i.e. the type of the data that will be returned by the
function with the return statement). But what if we want to return no
value?

• Imagine that we want to make a function just to show a message on the


screen. We do not need it to return any value. In this case we should use
the void type specifier for the function. This is a special specifier that
indicates absence of type.
Void Function Example
• void can also be used in the function’s parameter list to explicitly
specify that we want the function to take no actual parameters when
it is called. For example, function printmessage could have been
declared as:
void printmessage (void)
{
cout << “I’m a function!”;
}

• Although it is optional to specify void in the parameter list. In C++, a


parameter list can simply be left blank if we want a function with no
parameters.
• What you must always remember is that the format for calling a
function includes specifying its name and enclosing its parameters
between parentheses. The non-existence of parameters does not
exempt us from the obligation to write the parentheses. For that
reason the call to printmessage is:
printmessage ();

• The parentheses clearly indicate that this is a call to a function and


not the name of a variable or some other C++ statement. The
following call would have been incorrect:
printmessage;

You might also like