0% found this document useful (0 votes)
3 views31 pages

Chapter 4

Chapter 4 of the document covers functions in C++, detailing their definition, types (built-in and user-defined), and components. It explains how to pass arguments to functions, including passing by value and by reference, as well as the concept of default arguments and recursion. The chapter emphasizes the importance of functions in making programs more manageable and easier to understand.

Uploaded by

su1401168
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)
3 views31 pages

Chapter 4

Chapter 4 of the document covers functions in C++, detailing their definition, types (built-in and user-defined), and components. It explains how to pass arguments to functions, including passing by value and by reference, as well as the concept of default arguments and recursion. The chapter emphasizes the importance of functions in making programs more manageable and easier to understand.

Uploaded by

su1401168
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

Department of Computer Science

Chapter 4: Function and Passing argument to function

April 15, 2026


Outline
▪ What is function?
▪ Types of function
▪ Components of function
▪ Function argument
▪ Recursive function

4/15/2026 2
Function
✓C ++ enables its programmers to break up a program into segments commonly known as
functions, each of which can be written more or less independently of the others.
✓ A function is a group of statements that together perform a task. Every function in the
program is supposed to perform a well-defined task.
✓ Every C++ program has at least one function, which is main(), and all the most trivial
programs can define additional functions.
✓A function is a block of code designed to tackle a specific problem.
✓One of the best ways to tackle a problem is to start with the overall goal, then divide this
goal into several smaller tasks.

4/15/2026 3
Syntax and Calling function

4/15/2026 4
A simple function example

4/15/2026 5
Function…
▪ Function makes a program much easier to read, test and debug

✓ In the figure, we can see that main() calls a function named func1().
✓ Therefore, main() is known as the calling function and func1() is known as the called
function.
✓ The moment the compiler encounters a function call, the control jumps to the statements that
are a part of the called function.
✓ After the called function is executed, the control is returned to the calling program.

4/15/2026 6
Function…
✓The main() function can call as many functions as it wants and as many times as it
wants.
✓For example, a function call placed within a for loop, while loop, or do–while loop
may call the same function multiple times till the condition holds true.
✓Not only main(), any function can call any other function.

4/15/2026 7
Why do we need functions?

✓Dividing the program into separate well-defined functions facilitates each function to be
written and tested separately.

✓ Understanding, coding, and testing multiple separate functions is easier than doing the
same for one big function.

✓ When a big program is broken into comparatively smaller functions, then different
programmers working on that project can divide the workload by writing different
functions.

✓ Like C/C++ libraries, programmers can also write their own functions and use them from
different points in the main program or any other program that needs its functionalities.
4/15/2026 8
C++ functions generally adhere to the following rules
▪ Every function must have a name.
▪ 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.
▪ All function names have one set of parenthesis immediately following them. This
helps you (and C++ compiler) differentiate them from variables.
▪ The body of each function, starting immediately after parenthesis of the function
name, must be enclosed by braces.

4/15/2026 9
Types of function
▪ We have two types of function in C++
1. Built-in functions
2. User-defined functions

4/15/2026 10
Built-in functions
▪ Built-in functions are also known as library functions.
▪ We need not to declare and define these functions as they are already
written in the C++ libraries such as iostream, cmath etc. We can
directly call them when we need.

4/15/2026 11
Example: C++ built-in function example

▪ Here we are using built-in function pow(x,y) which is x to the


power y. This function is declared in cmath header file so we
have included the file in our program using #include directives.

4/15/2026 12
User-defined functions
▪ This functions need to be declared and defined by the user. We can define
the functions anywhere in the program and then call/invok these functions
from any part of the code and any number of times in the program.

▪ Just like variables, it should be declared before using, functions also need to
be declared before they are called.

▪ The purpose of using a function is to make the program design process easy,
understandable and thereby avoiding ambiguity.

4/15/2026 13
User-defined functions…

▪ The functions that we declare and write in our programs are user-defined
functions.

4/15/2026 14
Example

4/15/2026 15
Components of function

▪ A C++ function definition consists of a function header and a function body.


▪ Function Declaration: tells the compiler about a function name and how to call
the function.
▪ The actual body of the function can be defined separately
▪ Return Type − A function may return a value.
▪ The return_type is the data type of the value the function returns. Some
functions perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
▪ Function Name − This is the actual name of the function.
▪ The function name and the parameter list together constitute the function
signature.

4/15/2026 16
Components of function…
▪ Parameters − A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument.
▪ The parameter list refers to the type, order, and number of the parameters of a
function. Parameters are optional; that is, a function may contain no
parameters.
▪ Function Body − The function body contains a collection of statements that
define what the function does.
▪ Function Calling: Calling a function means making the instruction of the
function to be executed. While creating a C++ function, you give a definition of
what the function has to do.

4/15/2026 17
Example

4/15/2026 18
Function Arguments
▪ If a function is to use arguments, it must declare variables that accept the values of
the arguments. These variables are called the formal parameters of the function.
▪ The formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
▪ While calling a function, there are two ways that arguments can be passed to a
function.
1. Passing By value
2. Passing By Reference

4/15/2026 19
Passing by value
▪ This method copies the actual value of an argument into the formal parameter of
the function. In this case, changes made to the parameter inside the function have
no effect on the argument.
▪ The value of the actual parameter is passed to formal parameters when we call the
function.
✓ In this method, the called function creates new variables to store the value of the
arguments passed to it. Therefore, the called function uses a copy of the actual
arguments to perform its intended task.
✓ If the called function is supposed to modify the value of the parameters passed to
it, then the change will be reflected only in the called function.

4/15/2026 20
Example

Output
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

4/15/2026 21
Passing By Reference
▪ This method copies the reference of an argument into the formal parameter. Inside the
function, the reference is used to access the actual argument used in the call. This means
that changes made to the parameter affect the argument.

✓In this method, we declare the function parameters as references rather than normal
variables.

✓ When this is done, any changes made by the function to the arguments it received are also
visible in the calling function.

✓ To indicate that an argument is passed using call by reference, an ampersand (&) is placed
after the type in the parameter list.
4/15/2026 22
Output
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

4/15/2026 23
Default Arguments
▪ In C++ programming, you can provide default values for function parameters.

▪ The idea behind default argument is very simple. If a function is called by passing
argument/s, those arguments are used by the function. But if all argument/s are not
passed while invoking a function then, the default value passed to arguments are
used.

▪ The default arguments are used when you provide no arguments or only few
arguments while calling a function. The default arguments are used during
compilation of program.

4/15/2026 24
Example

4/15/2026 25
Function recursion
▪ In many programming languages including C++, it is possible to call a function
from a same function. This function is known as recursive function and this
programming technique is known as recursion.

▪ A function is said to be recursive if a statement in the body of the function calls


itself. It is useful for many tasks, like sorting or calculate the factorial of numbers.

4/15/2026 26
Function recursion
▪ To understand recursion, you should have knowledge of two important aspects:

▪ In recursion, a function calls itself but you shouldn't assume these two functions are
same function. They are different functions although they have same name.

▪ Local variables are variables defined inside a function and has scope only inside that
function. In recursion, a function call itself but these two functions are different
functions (You can imagine these functions are function1 and function 2. The local
variables inside function1 and function2 are also different and can only be accessed
within that function.

4/15/2026 27
Example

4/15/2026 28
Thank You!

4/15/2026 29
Quiz 1: 10%
1. What is function and its uses? 4 points
2. Write the syntax for declaring function. 2 points
3. List and describe types of function. 4 points
▪ Bonus: 3 points
1. Write a function which calculates the area of rectangle. Note the
dimensions of the polygon should be provided from the user.

4/15/2026 30
Bonus solution
void AreaRectangle (int l,int w);
void main()
{
int length,width,area;
cout<<Enter Length and width of rectangle”;
cin>>length>>width;
AreaRectangle(length,width);
}
void AreaRectangle (int l,int w)
{
int area;
area=l*w;
cout<<“The area of rectangle is=”<<area;
}

4/15/2026 31

You might also like