Lecture 3 - Functions
Lecture 3 - Functions
BSCS 205
Introduction
• 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.
Syntax of a Function
• 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 parameter 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 { }.
• Here you have the first function example:
First Function Example
First Function
• In order to examine this code, first of all remember something said at
the beginning of this class: a C++ program always begins its execution
by the main function.
• So we will begin there.
• We can see how the main function begins by declaring the variable z
of type int.
• Right after that, we see a call to a function called addition.
• Paying attention we will be able to see the similarity between the
structure of the call to the function and the declaration of the
function itself some code lines above:
Passing Parameters of a Function
Passing Method Parameters…..
• The parameters and arguments have a clear correspondence.
• Within the main function we called to addition passing two values: 5 and 3, that
correspond to the int a and int b parameters declared for function addition.
• At the point at which the function is called from within main, the control is lost
by main and passed to function addition.
• The value of both arguments passed in the call (5 and 3) are copied to the local
variables int a and int b within the function.
• Function addition declares another local variable (int r), and by means of the
expression r=a+b, it assigns to r the result of a plus b.
• Because the actual parameters passed for a and b are 5 and 3 respectively, the
result is 8.
Passing Method Parameters
• The following line of code:
• return (r);
• finalizes function addition, and returns the control back to the function
that called it in the first place (in this case, main).
• At this moment the program follows it regular course from the same
point at which it was interrupted by the call to addition.
• But additionally, because the return statement in function addition
specified a value: the content of variable r (return (r);), which at that
moment had a value of 8.
• This value becomes the value of evaluating the function call.
Passing Method Parameters
Passing Method Parameters
• So being the value returned by a function the value given to the
function call itself when it is evaluated, the variable z will be set to the
value returned by addition (5, 3), that is 8.
• To explain it another way, you can imagine that the call to a function
(addition (5,3)) is literally replaced by the value it returns (8).
• The following line of code in main is:
cout << "The result is " << z;
• That, as you may already expect, produces the printing of the result
on the screen.
Function Overloading
• With function overloading, multiple functions can have the same
name with different parameters:
• Example
• int myFunction(int x)
• float myFunction(float x)
• double myFunction(double x, double y)
• Consider the following example, which have two functions that add
numbers of different type:
Function Overloading Example
• Example
#include <iostream>
using namespace std;
int plusFuncInt(int x, int y) {
return x + y;
}
double plusFuncDouble(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Function Overloading
Function Overloading
• Instead of defining two functions that should do the
same thing, it is better to overload one.
• In the example below, we overload the plusFunc
function to work for both int and double:
Example
int plusFunc(int x, int y) {
return x + y;
}
double plusFunc(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Function Overloading…
• Note: Multiple functions can have the same name as long as the
number and/or type of parameters are different.