0% found this document useful (0 votes)
51 views

CC103 6 Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

CC103 6 Functions

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

CC103 – Data Structures and Algorithms

C++ Functions
Reychelle G. Nabong, MSIT
Instructor
C++ Functions

A function is a block of code that performs a specific task.


Suppose we need to create a program to create a circle and color it. We can
create two functions to solve this problem:
• a function to draw the circle
• a function to color the circle

Dividing a complex problem into smaller chunks makes our program easy to
understand and reusable.
There are two types of function:
• Standard Library Functions: Predefined in C++
• User-defined Function: Created by users
C++ User-defined Function

• 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.
Function Declaration and Definition
A C++ function consist of two parts:

• Declaration: the return type, the name of the function, and


parameters (if any)
• Definition: the body of the function (code to be executed)
C++ Function Declaration

• the name of the function is greet()


• the return type of the function is void
• the empty parentheses mean it doesn't have any parameters
• Parameters act as the variable inside the function
• the function body is written inside {}
Calling a Function
we have declared a function named
greet(). To use the greet() function,
we need to call it.
Here's how we can call the above
greet() function.
Example 1: Display a Text
Example 2: Function with Parameters
• we have used a
function that has one
int parameter and one
double parameter.

• We then pass num1


and num2 as
arguments. These
values are stored by
the function
parameters n1 and n2
respectively.
Return Statement
we have used void in the function declaration. For example,

• This means the function is not returning any value.


• It's also possible to return a value from a function. For this, we need to
specify the returnType of the function during function declaration.
• Then, the return statement can be used to return a value from a
function.
Return Statement
Example 3: Add Two Numbers
• In the above program, the add() function is used to find the sum of two numbers.
• We pass two int literals 100 and 78 while calling the function.
• We store the returned value of the function in the variable sum, and then we print it.
Function Prototype
In C++, the code of function declaration should be before the function call. However, if we want
to define a function after the function call, we need to use the function prototype. For example,
• This provides the compiler with information about the function
name and its parameters. That's why we can use the code to call a
function before the function has been defined.

The syntax of a function prototype is:


Benefits of Using User-Defined Functions

• 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.
The C++ Functions
A C++ function or a subprogram is simply a chunk of C++ code that
has:
A descriptive function name: Examples
computeTaxes to compute the taxes for an employee
isPrime to check whether or not a number is a prime number

A returning value (for value returning function)


The computeTaxes function may return with a double number representing the
amount of taxes
The isPrime function may return with a Boolean value (true or false)

A Sub-process (for non-value returning function)


Execution of Processes or Statements
The C++ Standard Functions
A C++ standard function are groups in different libraries which can be
included in the C++ program

Example: SAME OUTPUT WITHOUT


USING FUNCTION
pow standard function in <cmath> library
int base=2, exponent=3,result=1;
cout << “2 raised to 3 is “ << pow(2,3); for (int exp=1; exp<=exponent; exp++) {
result *= base;
OUTPUT: }
cout << “2 raised to 3 is “ << result;
2 raised to 3 is 8
Visit https://www.programiz.com/cpp-programming/library-function for more
standard library functions
The C++ User-Defined Functions
A C++ User-defined functions are functions developed by the
programmer to implement in the program
FUNCTION HEADER.
Containing return value type, function
VALUE RETURNING FUNCTION. name, and parameters.

Example: int -> the return value type is integer


add -> the function name.
int num1, int num2 -> Parameters
int add(int num1, int num2) {
required by the function to perform the
int sumofnumbers = num1 + num2; task
return sumofnumbers;
FUNCTION BODY.
} The statements inside the
function (inside {} “curly
Usage: int sum = add(4,5); braces”)
OR cout << “The sum is “ << add(4,5);
The C++ User-Defined Functions
NON-VALUE RETURNING FUNCTION
Example: FUNCTION HEADER.
Containing void keyword, function name,
parameters (but not required).
void showHeader() {
cout << “Student Information System”; void -> keyword to define non-value
cout << endl; returning function
} showHeader -> the function name.

Usage:
FUNCTION BODY.
The statements to be executed inside the function
showHeader(); (inside {} “curly braces”)
The C++ User-Defined Functions
NON-VALUE RETURNING FUNCTION (sample with parameter)
void greetEmployee(string employeename) {
cout << “Student Information System” << endl;
cout << “Welcome ” << employeename;
}
Usage:
string employee = “Kim Seokjin”;
greetEmployee(employee);
Output:
Student Information System
Welcome Kim Seokjin
The C++ User-Defined Functions
FUNCTION PARAMETERS. A function may don’t have or can have 1
or more parameters to perform its task.
void employeeTax(string employeename, double salary, double tax_percent=0.12) {
cout << “Employee Name: ” << employeename << endl;
cout << “Salary: ” << salary << endl;
cout << “Tax Percent: ” << tax_percent << endl; OUTPUT:
double tax = salary * tax_percent;
cout << “Tax: ” << tax << endl; Employee Name: JIN
Salary: 30000
} Tax Percent: 0.12
Usage: Tax: 3600
employeeTax(“JIN”, 30000.00);
Employee Name: JUNGKOOK
employeeTax(“JUNGKOOK”, 50000.00, 0.15); Salary: 50000
Tax Percent: 0.15
Tax: 7500
The C++ User-Defined Functions
FUNCTION PARAMETERS. A function may don’t have or can have 1
or more parameters to perform its task.

Required parameters must be placed at the start and the optional parameters must be placed at the
ends part
CORRECT DECLARATION:

void employeeTax(string employeename, double salary, double tax_percent=0.12)

WRONG DECLARATION:
void employeeTax(double tax_percent=0.12, string employeename, double salary)
The C++ User-Defined Functions
Incorrect Usage

int add(int num1, int num2) {


int sumofnumbers = num1 + num2;
return sumofnumbers;
}
1. Incompatible data type -> string sum = add(4,5);
2. Insufficient parameters -> cout << “The sum is “ << add(4);
3. Incorrect data type parameters -> cout << “The sum is “ << add(4,’a’);
4. Too much parameters supplied -> int sum = add(4,5,6);
The C++ User-Defined Functions
The function prototype. declaration in C++ of a function, its name, parameters and
return type before its actual declaration
Function Prototype.
Declared before the driver function (int
main)

Actual Declaration.
Placed after int main
driver.
Laboratory Exercise
Create a C++ program with the
following specification:

1. The user will be ask to input


arithmetic operation
2. The program will select the
function to run according to the
selected arithmetic operation
using switch
3. The program will ask the user if
he/she want to perform another
arithmetic operation.

You might also like