0% found this document useful (0 votes)
8 views

Chapter 5 C++

Uploaded by

asnak
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)
8 views

Chapter 5 C++

Uploaded by

asnak
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/ 26

Chapter Five

FUNCTIONS
Introduction
 a block of code that performs a specific task.
 a block of code which only runs when it is called.
 are the building blocks of C++
 should be declared before we use it

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
Function Declaration
• tells the compiler about a function name and how to call the
function.
• also called function prototype, consists of:
Syntax:
DataType functionName (parameter1, parameter2,...);

Example:-
/ / function declaration
int calcArea(int w, int h);

6
Example: reads two integers and displays their sum
line 1 #include<iostream.h> line 11 cout<<"SUM = "<<result;
line 2 int sum(int, int); // function line 12 }
declaration
line 3 int main() { line 13 int sum(int a, int b)
line 4 int var2,var1,result; line 14 {
line 5 result = 0; line 15 int c=0;
line 6 cout<<"Enter the first line 16 c = a + b;
number\n"; line 17 return c;
line 7 cin>>var1; line 18 }
line 8 cout<<"Enter the second number\n";
line 9 cin>>var2;
line 10 result = sum(var1,var2);

7
Calling a Function
• To use a function, you will have to call or invoke that function.
• When a program calls a function, program control is transferred to the
called function.
• To call a function, you simply need to pass the required parameters along
with function name, and if function returns a value, then you can store
returned value
int main() { function_name( );
/ / calling a function
calcArea(w,l);
}
8
User-Defined Functions
Function Definition
• Provides the actual body of the function
Syntax:
DataType functionName (parameter1, parameter2,...) {
/ / function body
}
Example:-
/ / function declaration
void greet() {
cout << "Hello World";
}

9
10
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
11
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.

12
🞂​ 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;
}

13
• 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.

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

15
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.
16
🞂​ 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.

17
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.
18
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.

19
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
}

20
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.
}

21
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.

22
Pass by Values

• A copy of the variable is passed


• Change in the copy of the variable doesn’t modify the original value
• copies of their values but never the variables themselves

23
Pass By Reference
• A variable itself is
passed
• any change done to the
variable affects the
values of the actual
parameters.

🞂​ This can be useful when


you need to change the
value of the arguments

24
// passing parameters by reference
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c){ a*=2;//a=a*2
b*=2;
c*=2;}
int main (){
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" <<z;
return 0;
}

25
Thank You

26

You might also like