0% found this document useful (0 votes)
33 views27 pages

CSC404 Chapter2 Part1

The document discusses functions in C++. It defines functions as groups of statements that perform a particular task. There are two types of functions: predefined functions that come with libraries and user-defined functions created by the programmer. User-defined functions can return a value, have no return value (void), or pass parameters by reference. Functions make programs easier to read and maintain by breaking them into smaller, focused tasks.
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)
33 views27 pages

CSC404 Chapter2 Part1

The document discusses functions in C++. It defines functions as groups of statements that perform a particular task. There are two types of functions: predefined functions that come with libraries and user-defined functions created by the programmer. User-defined functions can return a value, have no return value (void), or pass parameters by reference. Functions make programs easier to read and maintain by breaking them into smaller, focused tasks.
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/ 27

CHAPTER 2:

Functions

Prepared f or:

CSC 404 – Programming II


OBJECTIVES OF THIS CHAPTER

In this chapter, you will learn about:


Predefined functions and how to use them in a program
User-defined functions
Function s data t pe: void, int, float, double, char, char[]
Learn how to use function with parameter (value and reference)
The scope of global and local variables
INTRODUCTION TO FUNCTIONS

What is a FUNCTION?

Function is a group of statements that


will perform a particular task

For example:
To calculate the area of a rectangle
To display the total price of items purchased
WHY DO YOU NEED FUNCTIONS?

When you are solving a complex problem,


your main program can become long and complicated

Functions can be used to simplify your “main program

How can you simplify your main program?


By breaking up your long and complicated main program into
sub-programs
(known as FUNCTIONS)

These functions can be called using function call statements when needed
MAIN PROGRAM WITHOUT FUNCTIONS

What are the objectives of


this program?
MAIN PROGRAM WITH FUNCTIONS
INTRODUCTION TO FUNCTIONS
Functions:
Since each function will do a specific task

Therefore, it is easier for you to trace errors and debug

A function can be called numerous times, anytime you need it

Therefore, it can prevent redundancy and saves time

There are 2 categories of functions:


1. Predefined Functions
2. User-defined Functions
PREDEFINED 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

Will CALL the


predefined function named pow
in ide he cmath header file
PREDEFINED FUNCTIONS

<iomanip>:
Allows you to manipulate how your output is displayed

Function Name Description


setw(x) Set field width to x
left Places the characters to the left
instead of the right
setprecision(x) Set the floating point precision to x
fixed Display floating point values in
decimal notation
EXAMPLE
USER-DEFINED FUNCTIONS

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 with a Return Value:


This type of function will return ONE value after all the statements
inside it have been executed
For example, inside the function definition:

Will return the value


inside the variable “number
to the main program if it is called
FUNCTION WITH A RETURN VALUE

Function with a Return Value:


To use this function, you must do the following:
1. Function Declaration (a.k.a. Function Prototype):
This is where you will declare the function that you want to use
2. Function Call:
This is where you will call the function that you want to execute
3. Function Definition:
This is where you will define what your function will do

You will need to include a RETURN VALUE, so that it can be


passed back to the main program that calls it
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)

You should follow the following format for function call:


FUNCTION WITH A RETURN VALUE
Function Call:
For example:
Let say you want to calculate the total of 2 numbers:
The main program will call the function
CalcTotal() and the value inside the variable
num1 and num2 will be passed to the function

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

The main program will call the function


CalcWage() and the value inside the variable
hoursWorked and hourlyRate will be passed to the function
FUNCTION WITH A RETURN VALUE
Function Definition:
Is where you will define what your function will do when it is called by
the main progam
This is placed outside of the main program
Your function should include a RETURN VALUE:
After all the necessary statements have been
executed to achieve a value that the program
wants, that value can then be returned to the
main program
FUNCTION WITH A RETURN VALUE
Function Definition:
To define a function, you need these 4 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 along with its Type:
Is used to indicate what value the parameters received from
the main program (The order of the parameters is important)
The type of value must also be included, which can be of any data
type: int, double, float, char, char[]
4. Function Body:
Are the statements inside the function that needs to be executed
to perform certain tasks
You need to include a RETURN VALUE at the end
FUNCTION WITH A RETURN VALUE
Function Definition:
You should follow the following format for function definition:

For example:
Let say you want to calculate the total of 2 numbers:

Define function CalcTotal() that receives 2


integer values from main program.
These values will then be stored inside
variable no1 and no2 respectively.
This function will return the value inside the
variable total back to the main program.
FUNCTION WITH A RETURN VALUE
Function Definition:
Let say you want to calculate the average of total marks of students:
Define function CalcAverage() that receives
1 floating-point and 1 integer value from main
program. These values will then be stored
inside variable total and number respectively.
This function will return the value inside the
variable average back to the main program.

Let say you want to calculate the wage of an employee:


Define function CalcWage() that receives 1
integer and 1 floating-point value from main
program. These values will then be stored
inside variable hours and rate respectively.
This function will return the value inside the
variable wage back to the main program.
QUICK EXERCISE
What would be the output displayed on the screen for the program below?
QUICK EXERCISE

1. Write a program that will add up 2 numbers received from the user.

2. Write the same program, but use:


Function CalcTotal() that will receive 2 numbers through its
parameters. It then calculates the total of 2 numbers and returns
the total to the user.

You might also like