Chapter Two User Defined Function
Chapter Two User Defined Function
(Using C++)
Chapter Two
Function in C++(User Defined Functions)
Compiled by
Ataklti N.
1
2.1 What is Function in C++?
A function is a block of code that performs a specific task/ used to perform
certain actions.
A function is a group of statements that together perform a task.
Every C++ program has at least one function, which is main().
And these can be called anytime in the main function without typing the
whole code:
Define the code once, and use it many times.
Why Functions?
Functions make the code reusable.
Functions help us in reducing code redundancy.
Functions make the program easier as each small task is divided into a
function.
Functions increase readability, easy for understanding, writing
2 debugging.
Continued…
A large program maybe divided into suitable small segments and these
segments can be separately compiled, tested and verified.
So, division of a very large program into manageable smaller functions
or modules.
The purpose of a function is to receive data, process it and return a
value to the function which has called it.
The function in C++ language is also known as procedure, subroutine,
method in other programming languages.
3
Continued…
Some functions return a numeric value to the calling function.
These are called return type functions.
If a function does not return a numeric value we call it void
function.
A programmer should understand how to declare a function,
define a function and call a function.
The functions which are declared and defined by a programmer
are called user defined functions or programmer defined
functions.
Besides, C++ Standard Library has a large collection of
4 predefined functions which can also be used by a programmer.
Types of Functions
5
2.2.1 User Defined Functions
C++ allows the programmer to define their own function.
A user-defined function groups code to perform a specific task
and that group of code is given a name (identifier).
The Component of User defined Functions are:-
– Function declaration
– The definition of a function
– Calling functions
6
2.2.2 Function Prototype
A function prototype comprises type and name of the function
along with list of types of its parameters enclosed in brackets.
It ends with a semicolon
Syntax: type identifier (type-of- parameter1, type-of-
parameter2, … type-of- parameter n );
The prototype conveys the following information to the compiler.
(i) The type of function, i.e. the type of return value.
(ii) Number of parameters of the function.
(iii) Type of each parameter.
(iv) The order in which the arguments would be placed when the
function is called.
7
Continued…
The names of parameters may or may not be given in the
prototype, however the names are necessary in the head of
function in the function definition.
For calling the function in the main() or in any other function you
need to mention the name of function followed by arguments
(values of parameters) in parentheses and end the line by a
semicolon.
Therefore, it is again reminded that data arguments be given in the
same order and type as given in the declaration of function
prototype.
If this rule is not followed the program will give erroneous results.
8
Continued…
9
Continued…
10
2.2.2 C++ User-defined Function Types
11
2.3 Function Arguments
Passing arguments : An argument is a piece of data that is passed to
the function from a program.
Passing these arguments can be done in two ways:
Call 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.
Call 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.
12 This means that changes made to the parameter affect the argument.
2.4 Scope of Variables in C++
Variable Scope is a region in a program where a variable is
declared and used.
Variables and Storage Classes: The storage class of variables
determines the which parts of the program can access it and how
long it stays in existence.
Lifetime: The time period between the creation and destruction
of a variable is called its lifetime(or sometimes its duration)
Visibility: A variable’s visibility or scope describes the part of
the program where the variable is visible.
The major storage classes are auto/local, register, static and
global.
13
Local Variables
16
2.5 C++ Function Overloading
In C++, two functions can have the same name if the number
and/or type of arguments passed is different.
These functions having the same name but different arguments
are known as overloaded functions.
Note: In C++, many standard library functions are overloaded.
– For example, the sqrt() function can take double ,float, int,
as parameters.
17
2.6 C++ Programming Default Arguments
(Parameters)
In C++ programming, we can provide default values for function
parameters.
If a function with default arguments is called without passing
arguments, then the default parameters are used.
However, if arguments are passed while calling the function, the default
arguments are ignored.
the defaults must be added from right to left, i.e., a default value to a
particular argument in the middle of argument is not allowed.
Examples:
int mul(int i, int j=5, int k=10); // legal
int mul(int i = 5, int j); // illegal
int mul(int i=0, int j, int k=10); // illegal
18
2.7 C++ Recursion
3. If a function is recursive.
A. Actual parameter
B. Declared parameter
C. Local Variables
D. Global Variables
2.Which of the following function / types of function cannot have default parameters?
A. Member function of class
B. Main function
C. Member function of structure
D. user defined function
24 01/22/25
…Continued
3. Which of the following statement is correct about default arguments?
A. Only one parameter of a function can be a default argument.
B. Minimum one parameter of a function must be a default argument.
C. All the parameters of a function can be default argument.
D. No parameter of a function can be default.
4. Which of the following function declaration using default arguments is correct?
A. int dfa(int x, int y =5, int z=10)
B. int dfa(int x=5, int y =10, int z)
C. int dfa(int x=5, int y, int z=10)
27