0% found this document useful (0 votes)
10 views12 pages

Chapter 2

Chapter 2 discusses the fundamental concepts of functions in C++, highlighting their role in modularity, code reusability, and readability. It covers function declaration, definition, parameters, and arguments, as well as the differences between passing by value and by reference. Additionally, the chapter introduces recursion as a technique for solving problems by having functions call themselves.

Uploaded by

ahebabnsr101816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Chapter 2

Chapter 2 discusses the fundamental concepts of functions in C++, highlighting their role in modularity, code reusability, and readability. It covers function declaration, definition, parameters, and arguments, as well as the differences between passing by value and by reference. Additionally, the chapter introduces recursion as a technique for solving problems by having functions call themselves.

Uploaded by

ahebabnsr101816
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter 2: Functions in C++

Prepared by Mrs Hana.M

Saturday, April 5, 2025


Basic Concept of Functions:

• In the context of programming, functions are fundamental building blocks


that encapsulate a set of instructions to perform a specific task. Here's an
explanation of the basic concept and the need for functions:
• Functions:
Functions are named blocks of code that can be called and executed when needed.They
can take inputs (parameters), perform operations, and return outputs.Functions help
break down a program into smaller, manageable pieces, promoting code reuse and
modularity.

Saturday, April 5, 202 2


5
• Modularity: Need for Functions:
• Functions allow you to break down complex tasks into smaller, more manageable units.Each
function can handle a specific task or calculation, making the code easier to understand and
maintain.
• Code Reusability:
• Once a function is defined, it can be called multiple times throughout the program.This
promotes code reusability, as the same function can be used in different parts of the program
without rewriting the logic.
• Abstraction:
• Functions abstract away the implementation details of a specific task.Users of the function
only need to know what the function does (its interface) without needing to understand how it
accomplishes the task (its implementation).
• Readability: Saturday, April 5, 202
5
3

• Functions enhance the readability of the code by breaking it into logical units.Each function
In summary
• functions play a crucial role in structuring programs, promoting
reusability, enhancing readability, and managing complexity.
• By encapsulating logic within functions, programmers can develop more
organized and maintainable codebases.

Saturday, April 5, 202 4


5
Function Declaration:
• Declaring a function informs the compiler about the function's
name, return type, and parameters.
• The function declaration includes the function's prototype, which
consists of the function's name, return type, and parameter list.
• Function declarations typically occur before their first use in the
program.
• Function declaration
• int add(int a, int b);
Saturday, April 5, 202 5
5
Function Definition
• Defining a function involves providing the actual implementation of the
function.
• The function definition includes the function's return type, name, parameter
list, and the function body where the logic is written.
• Function definitions contain the code that executes when the function is called.
• Function definition
• int add(int a, int b) {
• return a + b;
• }

Saturday, April 5, 202 6


5
Function components

• Function Parameters:
• Parameters are variables declared in the function's declaration or
definition.
• They act as placeholders to receive values that are passed into the
function when it is called.
• Parameters are defined within the parentheses following the function
name.
• Function declaration with parameters
• int add(int a, int b);
Saturday, April 5, 202 7
5
Function Arguments
• Arguments are actual values or variables passed to a function when it is
called.
• These values are assigned to the function parameters during the
function call.
• Arguments are provided within the parentheses when calling a function.
• int num1 = 5;
• int num2 = 3;
• int sum = add(num1, num2); // Passing num1 and num2 as arguments to
the add function
Saturday, April 5, 202 8
5
In C++, functions are blocks of code that perform a specific task. They are essential for code organization,
modularity, and reusability.
#include <iostream>
// Function declaration to add two numbers
int add(int a, int b) //parameter
{
return a + b;
}
int main() {
int num1 = 5;
int num2 = 3;
// Function definition to Calling the add function and storing the result in sum
int sum = add(num1, num2);// num1,num2 Arguments
std::cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << std::endl;
return 0;
Saturday, April 5, 202 9
5
}
Calling function by value and reference
• Passing by Value:
• When passing parameters by value, a copy of the argument's value is passed to the function.
• Any modifications made to the parameter within the function do not affect the original argument outside the function.
• Changes made to the parameter are local to the function.
• void incrementByValue(int num) {
• num += 1;
• }
• int main() {
• int number = 5;
• incrementByValue(number); // Passing 'number' by value
• // 'number' remains unchanged here
• return 0;
• }

Saturday, April 5, 202 10


5
Passing by Reference
• When passing parameters by reference, the function receives a reference to the original argument.
• Changes made to the parameter within the function directly affect the original argument.
• Passing by reference is useful when you want a function to modify the value of the original argument.
• void incrementByReference(int &num) {
• num += 1;
• }
• int main() {
• int number = 5;
• incrementByReference(number); // Passing 'number' by reference
• // 'number' is incremented to 6
• return 0;
• }
Saturday, April 5, 202 11
5
Recursion in Functions

• Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem.
• It involves breaking down a problem into smaller, simpler subproblems until a base case is reached.
• #include <iostream>
• // Recursive function to calculate the factorial of a number
• int factorial(int n) {
• if (n == 0) {
• return 1; // Base case: factorial of 0 is 1
• } else {
• return n * factorial(n - 1); // Recursive call to calculate factorial
• }}
• int main() {
• int num = 5;
• int result = factorial(num);
• std::cout << "Factorial of " << num << " is: " << result << std::endl;
• return 0; }
Saturday, April 5, 202 12
5

You might also like