C++ Presentation
C++ Presentation
FUNCTION.
Definition of Function.
A function definition consists of two parts: interface (prototype) & body. The brace of a
function contains the computational steps (statements) that computerize the function.
If function definition is done before the main function, then there is no need to put the
prototype, otherwise the prototype should be scripted before the main function starts.
1
SUMMARIZED FUNCTION
BASICS.
C++ functions generally adhere to the following rules.
1. Every function must have a name.
following the same rules that apply to naming variables. They can
contain up to 32 characters, they must begin with a letter, and they can
1. Function Name: This is the identifier you use to call the function.
2. Parameters (or Arguments): These are the inputs that the function takes
4
MAIN COMPONENT OF FUNCTION
1. Function Body: This is the block of code that defines what the function does. It is
2. Return Statement: This specifies the value that the function outputs. Not all
functions need to return a value; some may perform an action without returning
anything.
3. Function Signature: This includes the function's name and the list of its
parameters. It provides a clear outline of what the function expects as inputs and
what it will return.
5
TYPES OF FUNCTION WITH EXAMPLE.
2. User-Defined Functions:
o These are functions created by the programmer to perform specific tasks.
o Example in Python:
python
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
7
TYPES OF FUNCTION WITH EXAMPLE
3. Lambda Functions:
o These are small anonymous functions defined using the lambda keyword.
o Example in Python:
python
add = lambda x, y: x + y
print(add(2, 3))
8
TYPES OF FUNCTION WITH EXAMPLE
4. Recursive Functions:
o These are functions that call themselves to solve a problem.
o Example in Python:
python
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
9
TYPES OF FUNCTION WITH EXAMPLE
5. Higher-Order Functions:
o These are functions that take other functions as arguments or return them.
o Example in Python:
python
return func(value)
print(apply_function(lambda x: x * x, 5))
10
6. Generator Functions:
o These use the yield keyword to return values one at a time, allowing
iteration through potentially large datasets without loading everything
into memory.
o Example in Python:
python
def countdown(num):
while num > 0:
yield num
num -= 1
for value in countdown(5): 11
DECLARATION OF FUNCTION.
Declaring a function varies slightly depending on the programming language, but the general
concept remains the same: you specify the function's name, parameters, and the block of code that
makes up its body.
name and returns a greeting message. The syntax differs slightly between
languages, but the core components of function declaration—name,
parameters, and body—are consistent.
14
PASSING VALUES OF FUNCTION BY VALUE WITH
EXAMPLE.
copy of the argument, so any changes made to the parameter inside the
function do not affect the original argument.
some examples:
15
#include <iostream>
using namespace ::std;
void increment(int value) {
value += 1;
}
int main() {
int num = 5;
increment(num); // Passing by value
cout << "Value of num after function call: " << num << endl; // Output will be 5
return 0;
16
}
Passing by value:
A value parameter receives a copy of only the value of the argument passed to it. As a result, if the function makes
any changes to the parameters, this will not affect the argument. For instance: #.....
{ Num = 0;
int main(void)
{ int x = 10;
Foo(x);
void(); 17
Passing By Value:
When the function is called and x passed to it, num receives a copy of the
value of x. As a result, although num is set to 0 by the function, this does not
affect x. the program produces the following output: Num = 0
x = 10
Passing arguments in this way, where the function creates copies of the
18
Passing By Reference
19
Taking the same example above:
#include<iostream>
using namespace ::std;
void Foo(int & num)
{
num = 0;
cout<< "num ="<< num << "\n";
}
int main(void)
{
int x = 10;
Foo(x);
cout<< "x="<<x<< "\n";
void(); 20
THANK YOU
21