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

Week 01 Functions

The document discusses functions in object oriented programming. It covers topics like function declaration and definition, calling functions, passing parameters by value vs reference, recursion, and inline functions. Examples are provided to illustrate key concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Week 01 Functions

The document discusses functions in object oriented programming. It covers topics like function declaration and definition, calling functions, passing parameters by value vs reference, recursion, and inline functions. Examples are provided to illustrate key concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1

Object Oriented Programming


Week - 01

Semester- Fall 2023


2
Functions

 A function is a block of code which only runs when it is called.

 You can pass data, known as parameters, into a function.

 Functions are used to perform certain actions, and they are


important for reusing code: Define the code once, and use it
many times.
3
Functions . . .

C++ Function
 Example Explained. The syntax of the function is:

o myFunction() is the name of the function. void myFunction() {


// code to be executed
}
o void means that the function does not have a return
value.

o Inside the function (the body), add code that defines


what the function should do
o

4
Call a Function

C++ Function
 Example Explained. Inside main, call myFunction():
// Create a function
 Declared functions are not executed immediately. They void myFunction()
are "saved for later use", and will be executed later, {
when they are called. }
cout << "I just got executed!";

 Call a function, write the function's name followed by two int main()
parentheses () and a “semicolon ;”. {
myFunction(); // call the
 In the given example, myFunction() is used to print a function
text (the action), when it is called: return 0;
}

// Outputs "I just got executed!"


5
Function Declaration and Definition

 A C++ function consist of two parts:


void myFunction()
{
// declaration • Declaration: the return type, the name of the function,
// the body of the function and parameters (if any)
(definition)

} • Definition: the body of the function (code to be executed)


Continue . . . 6
 However, it is possible to separate the
declaration and the definition of the function -
for code optimization.
•Note: If a user-defined function, such
 You will often see C++ programs that have as myFunction() is declared after
function declaration above main(), and the main() function, an error will occur:
function definition below main(). This will
make the code better organized and easier to int main()
read: {
myFunction();
#include <iostream> return 0;
using namespace std;
}
// Function declaration
void myFunction();
// The main method void myFunction() {
int main() cout << "I just got executed!";
{ }
myFunction(); // call the function
return 0;
} // Error

// Function definition
void myFunction()
{
cout << "I just got executed!";
}
7
Call by Value

• Call by Value is a method in which it passes the copies of


actual parameters to the formal parameters inside the
function.

• It passes the copies, so even if there is any change in the


values inside the function, it will not reflect that change in
the actual values.
8
Call by Reference

• Call by Reference is a method in which it passes the


reference or address of the actual parameter to the function's
formal parameters.

• which means if there is any change in the values inside the


function, it reflects that change in the actual values.
9
10

 The compiler may not perform inlining in such circumstances as:


1. If a function contains a loop. (for, while and do-while)
2. If a function contains static variables.
3. If a function is recursive.
4. If a function return type is other than void, and the return statement doesn’t exist in a
function body.
5. If a function contains a switch or goto statement.
11

#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
12
Call by Reference (Example)

• In this example, you assigned the variable salary with


the value of 27000. It passes the reference of the
variable to the function increment. Inside the function,
it increments the value by 5000, and the output at line
27 would be 32000.
• Now, as you passed the reference of the value, it will
reflect the changes made in the actual value. That
means at line 36, the actual value will also get
incremented to 32000.
• This is the output of the above example, and you can
see that the actual value of variable salary has also
changed, i.e. salary before the increment.
13
Call by Value (Example)

• In this example, the only change is that here you are


passing the value through the Call by value method.
You passed the copy of the actual parameter to the
formal parameter of the function increment. Inside the
function, the value is incremented by 5000.
• Now, as you know, there is no change in the actual
value in the Call by value method. So the value of the
variable sal will not change at line 36, i.e. salary before
the increment. And only the output at line 27, i.e. salary
after increment, will change to 32000.
• This is the output of the above example, and you can
see there is no change in the actual value, i.e. 27000.
14
15

 If a function with default arguments is called without passing arguments, then the default parameters are used.
 However, if arguments are passed while calling the function, the default arguments are ignored.
16
Recursion

• Recursion is the technique of making a function call itself.

• This technique provides a way to break complicated


problems down into simple problems which are easier
to solve.

• The recursion continues until some condition is met.


17
INLINE Function

 C++ provides inline functions to reduce the function call


overhead. An inline function is a function that is
expanded in line when it is called. When the inline
function is called whole code of the inline function gets
inserted or substituted at the point of the inline function
call.
Example 1: Factorial of a Number Using
Recursion 18

You might also like