0% found this document useful (0 votes)
12 views39 pages

Lecture 5 - Functions - Part 1

c++ functions

Uploaded by

breeziocontact
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views39 pages

Lecture 5 - Functions - Part 1

c++ functions

Uploaded by

breeziocontact
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Lecture 5:

Functions (part 1)

Dr. Fadi Alzhouri

Concordia University

Dr. Fadi Alzhouri 1


Review

• Loops in C++
• While
• Do/while
• For
• Stream manipulation functions.

2 Dr. Fadi Alzhouri


What are Functions?
Function:
• a set of statements to perform a certain task.
• a block of code which only runs when it is called.
• aka: modules, methods, procedures, subroutine, or
subprograms)
int main() {
Statement;
function1(); // call the function
Statement;
function2(); // call the function
.
.
.
}

void function1() {
Statement;
}

void function2() {
Statement;
}
Dr. Fadi Alzhouri 3
The Use of Functions
• A piece of program to complete a specific task that can be used
several times by being called.

• Programs can be quite large; we need to break them down into


smaller functions

• Functions call other functions to complete specific tasks

• Avoiding repetition

• Functions' motivations?
• Divide and Conquer: Divide the problem into smaller pieces,
you conquer the complexity of the problem.
• Reusability: Can be used in more than one place in a program
or in different programs
Dr. Fadi Alzhouri 4
Functions types

I. Predefined Functions:
• C++ Standard Library (cout and cin)

II. User-Defined Functions:


1- Void functions (nonvalue-returning):
o no return type
o do not return a value
2- Value-returning functions:
o have a data type
o return only one value to caller

5 Dr. Fadi Alzhouri


Introduction to Functions

• How many numbers are divisible by 3 between 10 and 20,


between 35 and 50, and between 60 and 80?

6 Dr. Fadi Alzhouri


Introduction to Functions (cont.)

7 Dr. Fadi Alzhouri


Introduction to Functions (cont.)

Three
similar
loops

8 Dr. Fadi Alzhouri


Predefined Functions:
Example: Math Library Functions

• C++ has many libraries which are predefined that you can
simply use them

• The math library has tens of math functions that you can call
• You need to include the header file <cmath>
• E.g. sqrt, ceil, etc.

• You call a function by specifying its name, and the list of


parameters (arguments) it takes
• E.g. sqrt(4) will return the square root of 4

9 Dr. Fadi Alzhouri


Math Library Functions
Method Description Example
ceil( x ) rounds x to the smallest integer ceil( 9.2 ) is 10.0
not less than x ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0
(x in radians)
exp( x ) exponential function ex exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
fabs( x ) absolute value of x fabs( 5.1 ) is 5.1
fabs( 0.0 ) is 0.0
fabs( -8.76 ) is 8.76
floor( x ) rounds x to the largest integer floor( 9.2 ) is 9.0
not greater than x floor( -9.8 ) is -10.0
fmod( x, y ) remainder of x/y as a floating- fmod( 13.657, 2.333 ) is 1.992
point number
log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0
log10( 100.0 ) is 2.0
pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128
pow( 9, .5 ) is 3
sin( x ) trigonometric sine of x sin( 0.0 ) is 0
(x in radians)
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x tan( 0.0 ) is 0
(x in radians)
10
Example: Math Library Functions

Header file

Dr. Fadi Alzhouri 11


User-Defined Functions
• You can create your own functions
• Format for function definition
return-value-type function-name (parameter-list)
{
declarations and statements
} Function body

• Parameter list
• Comma separated list of parameters
• Data type needed for each parameter
• If no parameters, use void or leave blank

• Return-value-type
• Data type of result returned (use void if nothing returned)

12 Dr. Fadi Alzhouri


Function Definitions
• Example:
Define a function Call a function
Function signature

int square( int y ) square( x );


{ Parameters list Actual parameters
or arguments
return y * y;
}

• return keyword
• Returns data, and control goes to function’s caller
• If no data to return, use return;
• Function ends when reaches right brace
• Control goes to the caller

13 Dr. Fadi Alzhouri


Function Definitions (cont.)

• A Function may return a value or not.


• The return value type is the data type of the value the
function returns.
• If the function does not return a value, the return value type
is the keyword void.

14 Dr. Fadi Alzhouri


Call a Function

Function definition

15 Dr. Fadi Alzhouri


Call a Function (cont.)

Calling/invoking a Function

16 Dr. Fadi Alzhouri


void Functions

• A void function does not return a any value.

Example: write a program to find whether a number is prime


or not in c++.

17 Dr. Fadi Alzhouri


void Functions

18 Dr. Fadi Alzhouri


Function Prototypes
• The function must be declared first before it can be called.
• One way to ensure it is to place the declaration before all
function calls.
• Another way to approach it is to declare a function
prototype before the function is called.
• A function prototype tells compiler argument type and
return type of function.

• Example:
int square(int);
• Function takes an int and returns an int
• Prototype appears before main()

19 Dr. Fadi Alzhouri


Function Prototypes (cont.)
• Function prototype contains
• Return type (void if returns nothing)
• Function name
• Parameters (number and data type)
• Only needed if function definition after function call

• Prototype must match function definition


• Function prototype
double maximum( double, double, double );
• Definition
double maximum( double x, double y, double z )
{

}
20 Dr. Fadi Alzhouri
Function Prototypes (cont.)

21
Example: Min function

The second Function min takes 3 arguments (all int) and returns int value.

22
Example: Min function (cont.)

Algorithm 1

23
Example: Min function (cont.)

Algorithm 2

24
Default Arguments

• You can define default values for parameters in a


function.
• The default values are passed to the parameters when
a function is invoked without the arguments.
Set default values in the
prototype.

25 Dr. Fadi Alzhouri


Default Arguments (cont.)

• Function call with omitted parameters


• If not enough parameters, rightmost go to their defaults

• Set defaults in function prototype


int myFunction( int x = 1, int y = 2, int z = 3 );
• myFunction(3)
• x = 3, y and z get defaults (rightmost)
• myFunction(3, 5)
• x = 3, y = 5 and z gets default

26 Dr. Fadi Alzhouri


Parameter Passing
• Call by value/ Pass by value
• Copy of data passed to the function
• Changes to the copy do not change the original,
regardless of the changes made to the parameter
inside the function.
• Prevent unwanted side effects

27 Dr. Fadi Alzhouri


Parameter Passing (cont.)

• Call by reference/ Pass by reference


• Function can directly access data
• Changes affect original variables using during the call
• Use & after data type in prototype
• void myFunction( int & )
• Read “data is a reference to an int”

28 Dr. Fadi Alzhouri


Call by reference

X 10

29 Dr. Fadi Alzhouri


Call by reference

X 10
y

Dr. Fadi Alzhouri 30


Call by reference

X 5
y

Dr. Fadi Alzhouri 31


Call by reference

X 5
y

Dr. Fadi Alzhouri 32


Reference Variables

33
Reference Variables

34
Practice
Choose the correct answer for the following questions. Be
sure to select all correct choices.
1- What is the correct prototype?

a) int square(x);
b) int square(int x);
c) int square(int);
d) int int square(int &x);

Dr. Fadi Alzhouri 35


Practice (cont.)
2- Select the correct result?

a) Hello Adam
Hello Adam
b) Hello Adam
Hello Tom
c) Hello Tom
Hello Tom
b) Error

Dr. Fadi Alzhouri 36


Practice (cont.)
3- The result is ?

a) 1020
b) 2020
c) 1010
b) 2010

Dr. Fadi Alzhouri 37


Practice (cont.)
• Write a C++ program that simulates a calculator. You
can assume that your calculator accepts the following
functions:
• Addition, Subtraction, Division, Multiplication

• Create a function for each operations

• Test your program with the following data

a b operation
15 20 *
40 34 +
Etc.

Dr. Fadi Alzhouri 38


Future sessions
• Functions are continued
• Function scopes
• Recursive functions

Dr. Fadi Alzhouri 39

You might also like