0% found this document useful (0 votes)
12 views21 pages

C++ Presentation

The document provides an overview of functions in programming, detailing their definitions, components, and types, including built-in, user-defined, lambda, recursive, higher-order, and generator functions. It explains the declaration of functions and the difference between passing arguments by value and by reference. Examples in C++ and Python illustrate these concepts, emphasizing how functions operate and interact with parameters.

Uploaded by

Solomon Asfaw
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)
12 views21 pages

C++ Presentation

The document provides an overview of functions in programming, detailing their definitions, components, and types, including built-in, user-defined, lambda, recursive, higher-order, and generator functions. It explains the declaration of functions and the difference between passing arguments by value and by reference. Examples in C++ and Python illustrate these concepts, emphasizing how functions operate and interact with parameters.

Uploaded by

Solomon Asfaw
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/ 21

FUNCTION AND PASSING ARGUMENT TO

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.

2. Function names are made up and assigned by the programmer

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

consist of letters, numbers, and the underscore (_) character.


2
C++ FUNCTIONS GENERALLY ADHERE TO THE
FOLLOWING RULES.

3. All function names have one set of parenthesis immediately


following them. This helps you (and C++ compiler)
differentiate them from variables.

 4. The body of each function, starting immediately after

parenthesis of the function name, must be enclosed by


braces.
3
MAIN COMPONENT OF FUNCTION.
 A function typically has several main components that work together to achieve a specific
task.
Here are the primary components:

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

to operate on. They are specified in the parentheses following the


function name.

4
MAIN COMPONENT OF FUNCTION
1. Function Body: This is the block of code that defines what the function does. It is

enclosed in braces {} in many languages, or indented in Python.

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.

 Functions come in various types, depending on their use and behavior.

Here are some common types of functions with examples:


1. Built-in Functions:
o These are predefined functions provided by the programming
language.
o Example in Python: print()
python
print("Hello, World!")
6
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

def apply_function(func, value):

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.

 Here are examples of function declarations in a few different


programming languages:
 Python:
python

def greet(name): # Function declaration


 return f"Hello, {name}!" 12
DECLARATION OF FUNCTION.
#include <iostream>
using namespace std;
string greet(string name) { // Function declaration
return "Hello, " + name + "!";
}
int main() {
cout << greet("Alice") << endl;
return 0;
13
}
DECLARATION OF FUNCTION.

 In these examples, each function named greet takes a single parameter

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.

 Passing values to a function by value means that the function receives a

copy of the argument, so any changes made to the parameter inside the
function do not affect the original argument.

 This is a common mechanism in many programming languages. Here are

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: #.....

void Foo(int num)

{ Num = 0;

cout<< “num = ” << num << “ \n”;

int main(void)

{ int x = 10;

Foo(x);

cout<< “x = ”<<x<< “\n”;

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

arguments passed to it is called passing by value.

18
Passing By Reference

A reference parameter, on the other hand, receives the


argument passed to it and works on it directly. Any change
made by the function to a reference parameter is in effect
directly applied to the argument.
o Passing parameters in this way is called pass-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

You might also like