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

chapter-3(Functions)

Chapter 3 of DIT1123 covers the concept of functions in C++, emphasizing their role as reusable code blocks that enhance modularity and code optimization. It details the types of functions, including library and user-defined functions, and provides examples of various function categories and recursion. The chapter also introduces inline functions, explaining how they can improve performance by reducing function call overhead.

Uploaded by

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

chapter-3(Functions)

Chapter 3 of DIT1123 covers the concept of functions in C++, emphasizing their role as reusable code blocks that enhance modularity and code optimization. It details the types of functions, including library and user-defined functions, and provides examples of various function categories and recursion. The chapter also introduces inline functions, explaining how they can improve performance by reducing function call overhead.

Uploaded by

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

DIT1123- Object Oriented Programming with C++

CHAPTER 3

Kukutla Alekhya
Objectives

Understand the concept and purpose of functions as reusable blocks of code that
perform specific tasks.
Demonstrate the ability to define and declare functions with appropriate function
signatures, including parameter lists and return types.
Understand the scope and lifetime of variables within functions, including local
variables and parameters.
Functions

The function in C++ language is also known as procedure or subroutine in


other programming languages.
To perform any task, we can create function. A function can be called many
times. It provides modularity and code reusability.
Advantage of functions in C
There are many advantages of functions.
1) Code Reusability
2) Code optimization
It makes the code optimized, we don't need to write much code.
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C++ header files such as
ceil(x), cos(x), exp(x), etc.
2. User-defined functions: are the functions which are created by the C++ programmer,
so that he/she can use it many times. It reduces complexity of a big program and
optimizes the code.
Different categories of functions:

Function with no return type no arguments.


Function with return type and no arguments
Function with return and with arguments
Function with no return type and with arguments.

Syntax :
Function defintion
return type function name(datatype parameter...)
{
//code to be executed
}
Syntax of function call
Functionname(parameters);
Function with no return type and no parameters

#include <iostream>
using namespace std;
void func()
{

cout<<”hello”<<endl;
}
int main()
{
func();
func();
func();
cout<<”the program has ended”;
}
Function with no return statement and with parameters

#include <iostream>
using namespace std;
void change(int data);
int main()
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5;
}
Value of the data is: 3
Recursion

The function which calls the same function, is known as recursive


function.

#include<iostream>
using namespace std;
int main()
{
int factorial(int);
int fact,value;
cout<<"Enter any number: ";
cin>>value;
fact=factorial(value);
cout<<"Factorial of a number is: "<<fact<<endl;
return 0;
}
int factorial(int n)
{
if(n<0)
return(-1); /*Wrong value*/
if(n==0)
return(1); /*Terminating condition*/
else
{
return(n*factorial(n-1));
}
}
Output:
Enter any number: 5
Factorial of a number is: 120
Function with return statement and no parameters

#include <iostream>
using namespace std;

// non-void return type


// function to calculate sum
int sum()
{
int a=10,b=20;
int s1 = a + b;

// method using the return


// statement to return a value
return s1;
}
int main()
{

int res = sum();


cout << "The sum is " << res;
return 0;
}
Inline function

If make a function as inline, then the compiler replaces the function calling location with
the definition of the inline function at compile time.
Syntax for an inline function:
inline return_type function_name(parameters)
{
// function code
}

When the inline function is encountered, then the definition of


the function is copied to it. In this case, there is no control
transfer which saves a lot of time and also decreases the
overhead.
#include<iostream>
using namespace std;
class cal
{
inline int add(int a, int b)
{
return(a+b);
}
int main()
{
cal c;
cout<<"Addition of 'a' and 'b' is:"<<c.add(2,3);
return 0;
}
Thank you

14

You might also like