You are on page 1of 7

Subject: Data Structures and Algorithms Prepared by: Mr.

Marvin Ramos

C++ Functions
• A function is a block of code that performs a specific task.
• You can pass data, known as parameters, into a function.
Page | 1
• Functions are used to perform certain actions, and they are important for reusing code: Define the code once,
and use it many times.
• Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.

Advantages of Functions
• Easier to code -Easier to Modify -Easier to Maintain
• Reusability -Less Programming Time -Easier to Understand

Types of C++ Function


There are two types of function:
1. Standard Library Functions: Predefined in C++
2. 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.

User defined functions usually have the following form.


//include statement
//function prototype
//main() function
//function definitions

Function Prototype
• A function prototype is a declaration of a function that tells the program about the type of value returned by the
function, name of function, number and type of arguments.

Syntax – C++ Function

• Return Type − A function may return a value. The return_type is the data type of the value the function returns.
Some functions perform the desired operations without returning a value. In this case, the return_type is the
keyword void.
• Function Name − This is the actual name of the function. The function name and the parameter list together
constitute the function signature.
Subject: Data Structures and Algorithms Prepared by: Mr. Marvin Ramos
• Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter.
This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
• Function Body − The function body contains a collection of statements that define what the function does.
Page | 2
Call a Function
• To call a function, write the function's name followed by two parentheses () and a semicolon ;

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)

void myFunction() { // declaration


// the body of the function (definition)
}

C++ Function Parameters


• Information can be passed to functions as a parameter. Parameters act as variables inside the function.
• Parameters are specified after the function name, inside the parentheses. You can add as many parameters as
you want, just separate them with a comma.

C++ Function Parameters – Syntax


returnType functionName (parameter1, parameter2,...) {
// function body
}
Example1: Function with Parameters Example2: Function with Parameters
Subject: Data Structures and Algorithms Prepared by: Mr. Marvin Ramos

C++ Default Parameters


• You can also use a default parameter value, by using the equals sign (=). A parameter with a default value, is
often known as an "optional parameter".
Page | 3
C++ Multiple Parameters
• Inside the function, you can add as many parameters as you want. Note that when you are working with
multiple parameters, the function call must have the same number of arguments as there are parameters, and
the arguments must be passed in the same order.

C++ The Return Keyword


• The void keyword indicates that the function should not return a value. If you want the function to return a value,
you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the
function.

C++ The Return Keyword Example

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.

C++ Functions - Pass By Value

 In call by value, the value of actual parameter is copied


into the formal parameter.
 Any changes to formal parameter have no effect on the
actual parameter.
 By default, C++ used call by value.

C++ Functions - Pass By Value – Example


Subject: Data Structures and Algorithms Prepared by: Mr. Marvin Ramos

C++ Functions - Pass By Reference


• In call by reference, the reference (&) of actual
Page | 4 parameter is passed instead of value.

• So, changes mode on format parameter will reflect


back to actual parameter.

C++ Functions - Pass By Reference - Example

Pass Arrays as Function Parameters


• You can also pass arrays to a function.

void myFunction(int myNumbers[5]) {


for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
Note that when you call the function, you only need
} to use the name of the array when passing it as an
} argument myFunction(myNumbers). However, the full
declaration of the array is needed in the function
int main() { parameter (int myNumbers[5]).
int myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
Pass Arrays as Function Parameters – Syntax
returnType functionName(dataType arrayName[arraySize]) {
// code
}

C++ Library Functions


• Library functions are the built-in functions in C++ programming.
• Programmers can use library functions by invoking the functions directly; they don't need to write the functions
themselves.
• Some common library functions in C++ are sqrt(), abs(), isdigit(), islower(c), isupper(c), toupper(c), tolower(c),
etc.
C++ User-defined Function Types
• For better understanding of arguments and return in functions, user-defined functions can be categorized as:
• Function with no argument and no return value
• Function with no argument but return value
• Function with argument but no return value
Subject: Data Structures and Algorithms Prepared by: Mr. Marvin Ramos
• Function with argument and return value

Function with no argument and no return value


• These are the functions which neither takes any argument nor returns any value are in this category.
Syntax
Page | 5
void function_name(){
//block of codes
}
Example

Function with no argument but return value


• These are the functions which takes no argument but it returns value are in this category.
Syntax
return_type function_name(){
//block of codes
}
Example

Function with argument and no return value


• These are functions which takes argument but it does not returns any value are in this category.
Syntax
void function_name(parameter_list){
//block of codes
}
Example
Subject: Data Structures and Algorithms Prepared by: Mr. Marvin Ramos

Function with argument and return value


• These are the functions which takes argument and also returns value are in this category.
Syntax
Page | 6
return_type function_name(parameter_list){
//block of codes
}
Example

Which method is better?


• All four programs above gives the same output and all are technically correct program.
• There is no hard and fast rule on which method should be chosen.
• The particular method is chosen depending upon the situation and how you want to solve a problem.

C++ Function Overloading


• Function overloading is an example of polymorphism features.
• Function overloading allows us to have more than one function with the same name.

C++ Function Overloading - Rules


• To have function overloading the functions should differ by –
1. Number of parameter
2. Type of parameter
3. Sequence of type parameter

Example: Function Overloading

int plusFuncInt(int x, int y) {


return x + y;
}
Note: Multiple functions can have the
double plusFuncDouble(double x, double y) {
same name as long as the number
return x + y;
and/or type of parameters are
}
different.

int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Subject: Data Structures and Algorithms Prepared by: Mr. Marvin Ramos

Example: Overloading Using Different Types of Parameter

Page | 7

Example: Overloading Using Different Number of Parameters

You might also like