0% found this document useful (0 votes)
27 views30 pages

C++ Chapter Five

Chapter Five discusses functions in C++, highlighting their importance for code reusability and optimization. It covers types of functions, including built-in and user-defined functions, as well as function parameters, return statements, and the benefits of user-defined functions. Additionally, it explains concepts like default parameter values, multiple parameters, and the difference between call by value and call by reference.

Uploaded by

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

C++ Chapter Five

Chapter Five discusses functions in C++, highlighting their importance for code reusability and optimization. It covers types of functions, including built-in and user-defined functions, as well as function parameters, return statements, and the benefits of user-defined functions. Additionally, it explains concepts like default parameter values, multiple parameters, and the difference between call by value and call by reference.

Uploaded by

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

Chapter Five

FUNCTIONS
Introduction
🞂​ a block of code that performs a specific task.
🞂​ a block of code which only runs when it is called.
🞂​ Functions are used to perform certain actions, and they
are important for reusing code, define the code once, and
use it many times
Advantage of functions

🞂​ Code Reusability
• By creating functions in C++, you can call it many times.
🞂​ Code optimization
• It makes the code optimized, we don't need to write
much code.
2
Types of Functions
🞂​ There are two types of functions:
 Built-in Functions:
• Also called library functions
• functions which are declared in
the C++ header files such as
ceil(x), cos(x), exp(x) etc.

 User-defined functions:
• Functions which are created by
the C++ programmer
• It reduces complexity of a big
program and optimizes the code.
3
Example 5: C++ Program to Find the Square Root of a Number

4
Example

5
User-Defined Functions
🞂​ C++ allows the programmer to define their own function.
🞂​ When the function is invoked from any part of the program, it
all executes the codes defined in the body of the function.
Function Declaration
Syntax:
returnType functionName (parameter1, parameter2,...) {
/ / function body
}
Example:-
/ / function declaration
void greet() {
cout << "Hello World";
}
6
🞂​ Description
• the name of the function is greet()
• the return type of the function is void
• the empty parentheses mean it doesn't have any
parameters
• the function body is written inside {}
Calling a Function
• In the above program, we have declared a
function named greet(). int main() {
• To use the greet() function, we need to call it. / / calling a function
• Here's how we can call the above greet() greet();
function. }
7
8
Create a Function
🞂​ C++ provides some pre-defined functions, such as main(),
which is used to execute code.
🞂​ also create your own functions to perform certain actions.
🞂​ To create a function, specify the name of the function,
followed by parentheses ().
Syntax 🞂​ myFunction() is the name
void myFunction() { of the function
/ / code to be executed 🞂​ void means that the
} function does not have a
return value.
🞂​ Body of the function, add
code that defines what the
9 function should do.
C a ll a Function
🞂​ Declared functions are not executed immediately. They
are "saved for later use", and will be executed later, when
they are called.
🞂​ To call a function, write the function's name followed by two
parentheses () and a semicolon ;
🞂​ Example : / / Create a function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); / / call the function
return 0;
}
• A function can be called multiple times
10
Function Parameters
🞂​ a function can be declared with parameters (arguments).
🞂​ A parameter is a value that is passed when declaring a
function.

Example:- let u s consider the function below:


void printNum(int num) {
cout << num;
}
🞂​ Here, the int variable n u m is the function parameter.

11
🞂​ We pass a value to the function parameter while calling the
function.
int main() {
int n = 7;
/ / calling the function
/ / n is passed to the function as argument
printNum(n);
return 0;
}

12
• In the above program, we
have used a function that
has one int parameter
and one double
parameter.

• We then pass num1 and


num2 as arguments.
These values are stored
by the function
parameters n1 and n2
respectively.

13
Note:
• The type of the
arguments passed
while calling the
function must
match with the
corresponding
parameters defined
in the function
declaration.

14
Return Statement
🞂​ In the above programs, we have used void in the function
declaration.
Example:
void displayNumber() {
/ / code
}
🞂​ This means the function is not returning any value.
🞂​ To return a value from a function, we need to specify the
returnType of the function during function declaration.
🞂​ The return statement can be used to return a value from a
function.
15
🞂​ Example:-
int add (int a, int b) {
return (a + b);
}
🞂​ Here, we have the data type int instead of void. This means
that the function returns an int value.
🞂​ The code return (a + b); returns the s u m of the two
parameters as the function value.
🞂​ The return statement denotes that the function has ended.
Any code after return inside the function is not executed.

16
Example 3: Add Two Numbers

In the above program, the add()


function is used to find the sum
of two numbers.

We pass two int literals 100 and


78 while calling the function.

We store the returned value of


the function in the variable
s u m , and then we print it.

Notice that sum is a variable of int type.This is because the return value
of add() is of int type.
17
Benefits of User-Defined Functions

• Code reusable:-
• We can declare them once
and use them multiple
times.
• Make the program easier as
each small task is divided into
a function.
• Functions increase
readability.

18
Parameters and Arguments
• Information can be passed to functions as a parameter.
• Parameters act as variables inside the function.
• Parameters are specified after the function name, inside the
parentheses.
• You can add as many parameters as you want, just separate them
with a comma.
Sy ntax:
void functionName (parameter1, parameter2,
parameter3) {
// code to be executed
}

19
Cont’d …
#include <iostream>
#include <string> • When a parameter is passed
using namespace std; to the function, it is called
an argument.
void myFunction(string fname) {
cout << fname << " Abebe\n"; • The value of a given
} parameters are called an
argument
int main() {
myFunction(“Yonas");
myFunction(“Abebe"); • From the example above: fname
myFunction(“Marat"); is a parameter, while Yonas,
return 0; Abebe and Marta are arguments.
}

20
Types of User-defined Functions

🞂​ User-defined A . No arguments and no return value


functions can be
categorized as a
function with:-
• no argument and
no return value
• no argument but
return value
• argument but no
return value
• argument and
return value

21
🞂​ Function with no argument but return value

22
🞂​ Arguments passed but no return value

23
Cont’d …

Arguments passed
and a return
value.

24
Default Parameter Value
🞂​ You can also use a default parameter value, by using the equals sign (=).

• If we call the function


without an argument, it
uses the default value
(“Ethiopia"):

A parameter with a default value, is often known as an "optional parameter".


Example: country is an optional parameter and “Ethiopia" is the default value.

25
Multiple Parameters
🞂​ Inside the function, you can add as many parameters as you want:

Note that when you are working with multiple parameters, the function call
must have the same number of arguments as there are parameters, and the
arguments must be passed in the same order.

26
Call by Value and Call by Reference
When calling a function in C++, arguments can be passed in two ways,

🞂​ by calling by
value

🞂​ by calling by
reference.

27
Return by Values
🞂​ The void keyword, used in the previous examples, indicates that the function
should not return a value.
🞂​ If you want the function to return a value, you can use a data type (such as int,
string, etc.) instead of void, and use the return keyword inside the function.
🞂​ uses normal variables when we passed parameters to a function.

28
Pass By Reference
🞂​ In C++, it is possible to
pass a value by reference
to the function.

🞂​ This can be useful


when you need to
change the value of the
arguments

29
Question

30

You might also like