CSC404 Chapter2 Part1
CSC404 Chapter2 Part1
Functions
Prepared f or:
What is a FUNCTION?
For example:
To calculate the area of a rectangle
To display the total price of items purchased
WHY DO YOU NEED FUNCTIONS?
These functions can be called using function call statements when needed
MAIN PROGRAM WITHOUT FUNCTIONS
Predefined Functions:
Are built-in functions
Allows you to use existing functions located inside the libraries,
so that o don t ha e to rite an code to perform a certain task
To use predefined functions in your program, you need to:
1. Use preprocessor directives
2. Include appropriate header file
EXAMPLE
Write a program that will receive a number and by using that
n mber, calc late n mber to the po er of 3
<iomanip>:
Allows you to manipulate how your output is displayed
User-defined Functions:
Are functions that are defined by the programmer
Function definitions can be written either:
In the same file as the main() program
In a separate file:
#include “fileName.cpp
USER-DEFINED FUNCTIONS
User-defined Functions:
There are 3 types of user-defined functions:
Function with a return value
(int, float, double, char, char[])
Function without a return value
(void)
Function with pass by reference
(int&, float&, double&, char&, char*&)
FUNCTION WITH A RETURN VALUE
Function Prototype:
All functions must be declared before they can be used in a program
Function declarations are placed just before the main program
FUNCTION WITH A RETURN VALUE
Function Prototype:
To declare a function, you need these 3 things:
1. The Return Type:
Is used to indicate the type of value that it will return
The type of value can be of any data type:
int, double, float, char, char[]
2. Name of the Function
3. List of Parameters Type:
Parameter: Is a container that will hold a value that will be
passed to the function
Is used to indicate what type of value the parameters will
receive and passed to the function
(The order of the parameters is important)
FUNCTION WITH A RETURN VALUE
Function Prototype:
You should follow the following format for function declarations:
For example:
Let say you want to calculate the total of 2 numbers:
Declare the function CalcTotal() that will receive
2 integer values and will return an integer value
Let say you want to calculate the average of total marks of students:
Declare the function CalcAverage() that
will receive 1 floating-point value 1 integer
value, and will return a floating-point value
Let say you want to calculate the wage of an employee:
Declare the function CalcWage() that will
receive 1 integer value and 1 floating-point
value, and will return a floating-point value
FUNCTION WITH A RETURN VALUE
Function Call:
Is performed to call a function that will do intended tasks
This is placed inside the main function OR even inside other functions
Since the function that ou e called ill return a alue, ou ould
need to store the value received into a variable
FUNCTION WITH A RETURN VALUE
Function Call:
To call a function, you need these 2 things:
1. Name of the Function
2. List of Parameters:
Is used to indicate what value the parameters will pass to
the function
(The order of the parameters is important)
Let say you want to calculate the average of total marks of students:
The main program will call
the function CalcAverage()
and the value inside the
variable totalMarks and
Let say you want to calculate the wage of an employee: numStudent will be passed
to the function
For example:
Let say you want to calculate the total of 2 numbers:
1. Write a program that will add up 2 numbers received from the user.