Lesson 2 Function 094128
Lesson 2 Function 094128
FUNCTION
Introduction
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.
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.
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.
•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 may require more code compared to recursive solutions, especially for
problems with complex patterns.
•In some cases, iterative solutions may be more efficient and faster than
recursive solutions.
Examples of Recursive Function Implementations
Factorial Function