Unit - 2(part-1)_functions & it's Types.pptx

sontibhanuprasad 5 views 19 slides Sep 16, 2025
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

functions


Slide Content

Unit - 2

Functions in C ++ A Function is a reusable block of code designed to perform a specific task. It helps break large programs into smaller, logical parts. Functions make code cleaner, easier to understand, and more maintainable . Just like in other languages, C++ functions can take inputs (called parameters), execute a block of statements, and optionally return a result. C ++ also supports advanced features like  function overloading ,  default arguments , and  inline functions , which give more flexibility compared to C.

Function Structure and Usage Function Syntax in C ++: Return_type function_name ( parameter_list ) { //body of the function } Each part has a specific role: Return type : Specifies what type of value the function returns. Use void if there is no return value. Function name : The name you will use to call the function. Parameter list : Inputs that the function accepts. It can be empty if no inputs are needed. Function body : The block of code that runs when the function is called.

Function Declaration vs Definition Function Declaration  introduces the function to the compiler. It tells the compiler the return type, name, and parameters but does not include the body. It ends with a semicolon . // Declaration int add( int , int ); Function Definition   provides the actual implementation of the function . int add( int a, int b) { return a + b; }

Calling a Function Once a function is defined, you can use it by simply calling its name followed by parentheses. This tells the program to execute the code inside that function. If the function takes parameters, you pass the required values inside the parentheses. If it doesn’t take any parameters, you just use empty parentheses. void greet() { cout << "Welcome to C++ Programming!" << endl ; } int multiply( int a, int b) { return a * b ; } int main() { greet(); int result = multiply(4, 5); cout << "Multiplication result: " << result << endl ; return 0; }

Types of Functions in C++ In C++, functions can be broadly categorized based on two criteria: 1. Based on origin: Library Functions : These are built-in functions provided by C++ standard libraries, such as  cout ,  sqrt () ,  abs() , and  getline () . You can use them by including appropriate  headers  like < iostream >, < cmath >, or <string>. User-Defined Functions : These are functions created by the programmer to perform specific tasks in the program .

2. Based on input and return type: User-defined functions can be further classified based on whether they accept parameters or return a value: No parameters, no return value:  The function performs a task but does not take input or return anything. Parameters, no return value:  The function takes input but does not return a result. No parameters, return value:  The function returns a result but does not take any input. Parameters and return value:  The function takes input and returns a result.

C++ Arrays

C++ Arrays An  array  is a collection of elements of the same type placed in contiguous memory locations. It allows you to store multiple values under a single name and access them using an index. Arrays are one of the most basic and commonly used data structures  in C++ programming

Create an Array We can create/declare an array by simply specifying the data type first and then the name of the array with its size inside  [] square brackets (better known as  array subscript operator ). Syntax: data_type array_name [size ]; Example: int arr [5]; This will create an array with name  arr   that can store  5 integers .

Initialize the Array Initialization means assigning initial values to array elements. We can initialize the array with values enclosed in  curly braces '{}'  are assigned to the array.  For example : int arr [5] = {2, 4, 8, 12, 16 }; int arr [5] = {2, 4, 8 }; int arr [] = {2, 4, 8, 12, 16 }; int arr [5] = {0};

Access Array Elements Elements of an array can be accessed by their position (called index) in the sequence. In C++, indexes of an array starts from 0 instead of 1. We just have to pass this index inside the  [] square brackets  with the array name as shown : array_name [index]; It is important to note that  index  cannot be negative or greater than size of the array minus 1.  (0 ≤ index ≤ size - 1 ). 

Update Array Elements To change the element at a particular index in an array, just use the  “=” assignment operator with new value as right hand expression while accessing the array element . array_name [index ] = value ; Example: int arr [] = {2, 4, 8, 12, 16}; arr [0 ] = 90;

Traverse Array Traversing means visiting each element one by one. The advantage of array is that it can be easily traversed by using a  loop  with loop variable that runs from 0 to size - 1. We use this loop variable as index of the array and access each element one by one sequentially . int arr [5] = {2, 4, 8, 12, 16 }; // Traversing and printing arr for ( int i = 0; i < 5; i ++) cout << arr [ i ] << " ";

Size of Array The size of the array refers to the number of elements that can be stored in the array. The array does not contain the information about its size but we can extract the size using  sizeof ()  operator.

string letters[2][2][2] = {   {     { "A", "B" },     { "C", "D" }   },   {     { "E", "F" },     { "G", "H" }   } }; Multi-Dimensional Arrays A multi-dimensional array is an array of arrays. To declare a multi-dimensional array, define the variable type, specify the name of the array followed by square brackets which specify how many elements the main array has, followed by another set of square brackets which indicates how many elements the sub-arrays have. string letters[2][4 ]; string letters[2][4] = {   { "A", "B", "C", "D" },   { "E", "F", "G", "H" } };

Types of Multidimensional Arrays We can have any number of dimensions in an array as per requirement, but the complexity of handling them also increases exponentially. That is why, the most widely used multidimensional arrays are: Two-Dimensional Array (2D Array) Three-Dimensional Array (3D Array) 2D Array: A  two-dimensional array  in C++ is a collection of elements organized the form of rows and columns. It can be visualized as a table or a grid . 3D Array: A three-dimensional array in C++ is a collection of elements organized in a 3D cuboid-like structure. It can be visualized as a series of two-dimensional arrays stacked on top of each other.

3-D Array:
Tags