functions
functions
NUML
C++ Functions
2
Types of Function
3
1. User-defined Function
10
Continue…..
Return Statement
12
Continue…..
13
Example 3: Add Two Numbers
14
Continue…..
15
Function Prototype
16
Continue….
17
Example 4: C++ Function Prototype
18
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.
19
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(), isdigit(),
etc.
• In order to use library functions, we usually need to include the
header file in which these library functions are defined.
• For instance, in order to use mathematical functions
such as sqrt(), isdigit()we need to include the header file
cmath.
20
Example 5: Program to Find the
Square Root of a Number
21
Continue…..
22
C++ Pass by Value vs. Pass by Reference
• In above examples, we learned about passing
arguments to a function. This method used is called
passing by value because the actual value is passed.
• However, there is another way of passing arguments
to a function where the actual values of arguments
are not passed. Instead, the reference to values is
passed.
23
C++ Pass by Value vs. Pass by
Reference
24
C++ Pass by Value vs. Pass by Reference
25
Example 6: Passing by reference
26
Continue….
27