1. FUNCTION IN C++
GUIDED BY
PREPARED BY
MR.MITESH JAISWAL
AKSHAY JANI
(116380307521) LCTURER IN
KAUSHAL SONI PIET(DS) 2ND SHIFT
(116380307509)
2. WE ARE IMMENSLY GREATFUL TO OUR LECTURER AND PROJECT
GUIDE, MR.MITESH JAISWAL, FOR HER INVALUABLE GUIDANCE
WHICH GAVE US A DEEP INSIGHT ON THE SUBJECT. WITH HER
KEEN INTEREST AND CONSTANT MORAL BOOSTING, WE ARE
ABLE TO IMPLEMENT THE PROJECT SATISFACTORILY.
WE EXPRESS OUR VERY SINCERE THANKS TO COMPUTER
ENGINEERING DEPT. FOR PROVIDING ADEQUATE FACILITIES TO
COMPLETE OUR PROJECT.
WE ARE AGAIN CORDIALLY THANKFUL TO OUR C&E
DEPARTMENT STAFF, FRIENDS AND OTHER PEOPLE WHO HAVE
DIRECTLY OR INDIRECTLY HELPED US IN COMPLETION OF THIS
PROJECT.
LAST BUT NOT THE LEAST, WE ARE ALSO THANKFUL TO OUR
FAMILY MEMBERS WHO ENGOURAGED AND SUPPORTED US
ROUND THE CLOCK FOR THIS PROJECT.
Akshay Jani
Kaushal Soni
3. CERTIFICATE
This is to certify that AKSHAY JANI Student of computer
Engineering, bearing Enrollment No: 116380307521 have satisfactorily
completed his/her Seminar work as a part of course curriculum in
Diploma Engineering semester III having a report title “ FUNCTIONS IN
C++ ”.
MR.MITESH JAISWAL
Lecturer, computer Dept.
PIETDS-2nd Shift, Limda.
PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY
COMPUTER ENGG. DEPARTMENT
LIMDA, VAGHODIA, VADODARA
4. This is to certify that KAUSHAL SONI Student of computer
Engineering, bearing Enrollment No: 116380307509 have satisfactorily
completed his/her Seminar work as a part of course curriculum in
Diploma Engineering semester III having a report title “ FUNCTIONS IN
C++ ”.
MR.MITESH JAISWAL
Lecturer, computer Dept.
PIETDS-2nd Shift, Limda.
PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY
COMPUTER ENGG. DEPARTMENT
LIMDA, VAGHODIA, VADODARA
5. FUNCTIONS IN C++
Experience has shown that the best way to develop and maintain large
programs is to construct it from smaller pieces(Modules)
This technique Called “Divide and Conquer”
Bad Development Approach Wise Development Approach
main()
main() •Easer To {
{ -----
----- Design ----
----- Build }
----- Debug
----- Extend function f1()
. Modify {
. Understand ---
. Reuse ---
---- Better Organization }
-----
----- function f2()
Return 0; {
} ---
---
}
6. FUNCTIONS IN C++(CONT.)
In FORTRAN Modules Known as Subprograms
In Pascal Modules known as Procedures &
Functions
In C++ Modules Known as Functions & Classes
Programs use new and “prepackaged” modules
New: programmer-defined functions and classes
Prepackaged: from the standard library
7. ABOUT FUNCTIONS IN C++
Functions invoked by a function–call-statement which consist of
it’s name and information it needs (arguments)
Boss To Worker Analogy
A Boss (the calling/caller function) asks a worker (the called
function) to perform a task and return result when it is done.
Boss
Main
Worker
Worker Worker
Function Z
Function A Function B
Worker Worker
Note: usual main( ) Calls other
Function B1 Function B2 functions, but other functions
can call each other
8. ADVANTAGES OF USING FUNCTIONS
1. To help make the program more
understandable
2. To modularize the tasks of the program
building blocks of the program
3. Write a module once
those lines of source code are called multiple
times in the program
8
9. ADVANTAGES OF USING FUNCTIONS
4. While working on one function, you can focus on
just that part of the program
construct it,
debug it,
perfect it.
5. Different people can work on different functions
simultaneously.
6. If a function is needed in more than one place in a
program, or in different programs, you can write it
once and use it many times
9
10. FUNCTION OVERLOADING
Function overloading
Functions with same name and different
parameters
Should perform similar tasks
I.e., function to square ints and function to square floats
int square( int x) {return x * x;}
float square(float x) { return x * x; }
A call-time c++ complier selects the proper function by
examining the number, type and order of the parameters
11. USING DEFAULT ARGUMENTS
When you don’t provide enough arguments in a function
call, you usually want the compiler to issue a warning
message for this error
Sometimes it is useful to create a function that supplies a
default value for any missing parameters
11
12. USING DEFAULT ARGUMENTS
Two rules apply to default parameters:
If you assign a default value to any variable in a function
prototype’s parameter list, then all parameters to the right of that
variable also must have default values
If you omit any argument when you call a function that has default
parameters, then you also must leave out all arguments to the right
of that argument
12
13. EXAMPLES OF LEGAL AND ILLEGAL
USE OF FUNCTIONS WITH DEFAULT
PARAMETERS
13
14. OVERLOADING FUNCTIONS
In most computer programming languages, each
variable used in a function must have only one
name, but C++ allows you to employ an alias
Similarly, in most computer programming
languages, each function used in a program must have
a unique name
You don’t have to use three names for functions that
perform basically the same task, C++ allows you to
reuse, or overload, function names
14
15. OVERLOADING FUNCTIONS
When you overload a function, you must ensure that the
compiler can tell which function to call
When the compiler cannot tell which version of a function to
use, you have created ambiguity
15