0% found this document useful (0 votes)
5 views50 pages

Lesson 2 Function 094128

This document provides an overview of functions in C++, highlighting their importance for code modularity, reusability, and maintainability. It covers the definition, creation, and usage of functions, including parameters, return types, and the return keyword, as well as the distinction between actual and formal parameters. Additionally, it discusses void functions, functions with return values, and recursive functions, emphasizing their role in organizing code and solving complex problems.

Uploaded by

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

Lesson 2 Function 094128

This document provides an overview of functions in C++, highlighting their importance for code modularity, reusability, and maintainability. It covers the definition, creation, and usage of functions, including parameters, return types, and the return keyword, as well as the distinction between actual and formal parameters. Additionally, it discusses void functions, functions with return values, and recursive functions, emphasizing their role in organizing code and solving complex problems.

Uploaded by

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

Lesson 2

FUNCTION
Introduction

 C++, an extension of the C language, is known for its


efficiency and control over system resources

 Functions in C++ play a crucial role in


structuring the code, making it modular,
reusable, and maintainable.

 This guide delves into the concept of functions in


C++, overing their creation, usage,paremeter
handling, and the use of the return keyword.
Introduction

Functions in C++ are blocks of code that perform


specific tasks and are essential for:

•Modularity: Breaking down complex problems


into smaller,
manageable parts.
•Code Reusability: Promoting the DRY (Don't
Repeat Yourself)
principle.
•Simplified Maintenance: Making the code
easier to test,
debug, and update.
Function Creation and Usage
Defining a Function

A function in C++ is defined with a return type, a name,


and parameters (if any). It can be part of a class (as a
method) or standalone
Parts of a function
•Return Type − A function may return a value.
The return_type is the data type of the value the function
returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is
the keyword void.

•Function Name − This is the actual name of the


function. The function name and the parameter list
together constitute the function signature.
•Parameters − A parameter is like a placeholder.
When a function is invoked, you pass a value to
the parameter. This value is referred to as actual
parameter or argument. The parameter list refers
to the type, order, and number of the parameters
of a function. Parameters are optional; that is, a
function may contain no parameters.
Function Body − The function body contains a
collection of statements that define what the
function does.
Calling a Function

 Functions are called by their name, with arguments passed to


them if they require parameters
Function Prototypes

In C++, function prototypes (declarations) are often used before


the main function to inform the compiler about the function's
existence.
Actual vs Formal Parameters

Parameters in C++ functions are crucial for passing


values:

•Formal Parameters: Specified in the function definition


and used within the function.
•Actual Parameters: The values passed to the function
at the time of calling.
Actual vs Formal Parameters
The return Keyword in Functions

The return keyword in C++ indicates the end of the


function's execution and optionally returns a value to the calling
function.

Returning Values: Functions should return a value of


their declared return type.

Void Functions: Function with no return value have a return


type of void.
Example

// function returning the max between two numbers

int max(int num1, int num2)


{
// local variable declaration
int result;

if (num1 > num2)


result = num1;

else

result = num2;
return result;

}
 Functions With No Parameters
and Return Values
In C programming, functions play a crucial role in
organizing code and improving code reusability.

Functions can be designed to accept arguments


(parameters) and return values, but there are cases
where functions may not require any arguments or still
return values without explicit arguments.
Creating Functions With No Arguments

Functions in C can be defined without any


parameters, meaning they do not require any
arguments to be passed when called.

These functions can perform tasks that do not


depend on external data and can be executed
independently
In this example, the
greetUser function
does not accept any
parameters.

It can be called
directly from main, and
its execution will result
in printing the greeting
message.
 Functions With No Parameters
but With Return Values
 Functions With No Parameters
but With Return Values
In C++ programming, functions are an essential construct
that allows you to organize code and promote reusability.

While some functions take arguments (parameters) to


perform their tasks, there are scenarios where functions may
not require any arguments but still return values.
.
Creating Functions With No Arguments but With Return
Values

Functions in C++ can be designed without any


parameters, meaning they do not require any
arguments to be passed when called.

However, these functions can still perform


computations and return results to the calling code
 Functions With Parameters and
No Return Values
In C++ programming, functions play a crucial role in
organizing and modularizing code.

Functions that accept arguments but do not return any value


are commonly used when we need to perform a specific
action or task without requiring a result to be returned.

These functions are known as "void functions" because they


have a return type of void, indicating that they do not return
any value
Syntax of Void Functions
Example: Void Function With Multiple Arguments
Just a Reminder

Creating functions that accept arguments but do not return any value
(void functions) is an essential concept in C++ programming. They
allow you to perform specific tasks or actions without the need to
retrieve a result. Void functions enhance the modularity and reusability
of your code by encapsulating functionality into smaller, manageable
units.
By using void functions, you can organize your code more efficiently
and make it easier to read and maintain. They play a vital role in
breaking down complex tasks into smaller, manageable parts, making
your programs more structured and easier to comprehend.

Remember to use void functions when you need to execute a task


without requiring a return value, and you'll find yourself writing more
efficient and maintainable C++ programs.
Functions With Parameters and Return Values
in C++ programming, functions are powerful tools that
allow you to create reusable blocks of code.

Functions that accept arguments and return values are


widely used to perform specific operations and produce
results that can be utilized in other parts of the program.

These functions enable you to pass data to them,


process it, and then return the computed result back to
the calling code.
Syntax of Functions With Return Values
Example: Function With Multiple Arguments and Return Value
In this example, the calculateSum function takes two integer arguments, a and b,
and returns their sum using the return statement.
Simple note!

Creating functions that accept arguments and return values is a


fundamental concept in C++ programming. These functions allow
you to write modular, reusable code by encapsulating specific
functionalities.

By using functions with return values, you can efficiently compute


results and make your code more organized and readable.

Utilizing functions with arguments and return values enhances the


overall design and maintainability of your C++ programs. They
provide a clean separation of concerns, making it easier to
understand, debug, and update your codebase
Recursive Functions
Recursive functions in C++ are functions that call
themselves during their execution.

They provide an elegant and powerful way to solve


complex problems by breaking them down into
smaller, more manageable subproblems.

Recursive functions allow you to express algorithms


in a concise and intuitive manner, making your code
more readable and maintainable.
Definition and Usage of Recursive Functions

A recursive function is a function that calls itself to solve a smaller


instance of the same problem.

Recursive functions require two important components:

•Base Case: A condition that defines the simplest form of the problem,
which can be directly solved without further recursion.

•Recursive Case: The function calls itself with a reduced version of the
original problem until it reaches the base case.
Recursive functions are particularly useful for tasks that exhibit self-
similarity or repetitive patterns.

They allow you to express complex algorithms in a more concise and


intuitive manner.
Recursive vs. Iterative Approaches

Both recursive and iterative approaches are used to solve


problems in programming.

The choice between them depends on the problem's nature


and requirements.
Recursive Approach:

•Recursive functions call themselves, which leads to


function calls placed on the stack.

•Recursive solutions are generally shorter and more


elegant compared to iterative solutions, expressing the
problem more naturally.

•They are useful for problems with self-similarity or


nested structures.

•However, excessive recursion can lead to stack overflow,


consuming more memory
Iterative Approach:

•Iterative solutions use loops to repeat a set of instructions until a condition is


met.

•They may require more code compared to recursive solutions, especially for
problems with complex patterns.

•Iterative solutions often use less memory compared to recursive solutions.

•In some cases, iterative solutions may be more efficient and faster than
recursive solutions.
Examples of Recursive Function Implementations
Factorial Function

The factorial of a non-negative integer n is the product of


all positive integers less than or equal to n.

The factorial of 0 is defined to be 1. We can compute the


factorial using a recursive function
Fibonacci Sequence

The Fibonacci sequence is a series of numbers in


which each number is the sum of the two preceding
ones.

The sequence starts with 0 and 1.

We can compute the nth Fibonacci number using a


recursive function:

You might also like