Professional Documents
Culture Documents
CP2 Module 6 - Functions in C++
CP2 Module 6 - Functions in C++
CP2 Module 6 - Functions in C++
COMPUTER PROGRAMMING 2 2
What is a Function?
• a block of code that performs a specific task.
• used to perform certain actions and important for reusing code
○ Define the code once, and use it many times.
• allows data to be passed on to a parameter
Two Types of Functions:
1. Standard Library Functions: Predefined in C++
2. User-defined Function: Created by users
COMPUTER PROGRAMMING 2 3
C++ User-defined functions
• C++ allows the programmer to define their own function.
• A user-defined function groups code to perform a specific
task and that group of code is given a name (identifier).
• When the function is invoked from any part of the
program, it all executes the codes defined in the body of
the function.
COMPUTER PROGRAMMING 2 4
Function Declaration
1. specify the returnType and functionName
2. specify the parameter(s) - if needed
3. enter the code to be executed in the function body
Function Syntax:
returnType functionName(parameter1, parameter2, …){
//function body
}
Example:
NOTE:
void keyword does not return a value
COMPUTER PROGRAMMING 2 5
Calling a Function
● Declared functions are not executed immediately
● Functions are stored and then called to be executed
● To call a function, write the function's name followed by two
parentheses () and a semicolon ;
Example:
Source: https://www.programiz.com/cpp-programming/function
https://www.w3schools.com/cpp/cpp_functions.asp
COMPUTER PROGRAMMING 2 6
Function Example:
● Create a function that displays a text.
Sample Code:
Output:
function
call
COMPUTER PROGRAMMING 2 7
Parameters in a Function
● A parameter is a value that is passed when declaring a function.
Example: parameter
function call
COMPUTER PROGRAMMING 2 8
Function with Parameters Example:
● Create a function that displays a number within a text .
Sample
Code: Output:
COMPUTER PROGRAMMING 2 9
Return Statement in a Function
● To return a value in a function use a data type in declaring the
function then use the return statement.
Example: does not display a value since
it uses the void keyword
Instead…
COMPUTER PROGRAMMING 2 10
Sample Exercises for Functions:
● Create a program that adds two numbers using a function
Sample
Code: Output:
COMPUTER PROGRAMMING 2 11
Let us try it out for Functions:
● Create a program the will subtract, multiply and divide two
numbers using a function
Output:
COMPUTER PROGRAMMING 2 12
Benefits of using a User-defined Function?
● Functions make the code reusable. We can declare
them once and use them multiple times.
● Functions make the program easier as each small
task is divided into a function.
● Functions increase readability.
COMPUTER PROGRAMMING 2 13
C++ Library Functions
● Library functions are the built-in functions in C++ programming.
● Programmers can use library functions by invoking the functions
directly
● Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.
● To include built-in math functions, use the <cmath> library
Sample
Code:
COMPUTER PROGRAMMING 2 14
Example for C++ Library Function:
● Create a program that calculates the square root of a
number using the standard library function.
Sample
Code:
Output:
COMPUTER PROGRAMMING 2 15
References:
• Pomperada, Jake. 2019. Beginner’s Guide to C++ Programming.
Manila: Mindshapers Co., Inc.
• Pepito, Copernicus. 2009. Introduction to C++ 2008 programming.
Manila: National Bookstore.
• https://www.codecademy.com/courses/learn-c-plus-plus
• https://www.w3schools.com/cpp/default.asp
• https://www.tutorialspoint.com/cplusplus/index.htm
COMPUTER PROGRAMMING 2 16
COMPUTER PROGRAMMING 2 17
COMPUTER PROGRAMMING 2