c++ 2

18
FUNCTION IN C++ PREPARED BY AKSHAY JANI (116380307521) KAUSHAL SONI (116380307509) GUIDED BY MR.MITESH JAISWAL LCTURER IN PIET(DS) 2 ND SHIFT

Upload: jani

Post on 12-Jul-2015

75 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: C++ 2

FUNCTION IN C++

PREPARED BY

AKSHAY JANI

(116380307521)

KAUSHAL SONI

(116380307509)

GUIDED BY

MR.MITESH JAISWAL

LCTURER IN

PIET(DS) 2ND SHIFT

Page 2: C++ 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

Page 3: C++ 2

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 JAISWALLecturer, computer Dept.

PIETDS-2nd Shift, Limda.

PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY

COMPUTER ENGG. DEPARTMENT

LIMDA, VAGHODIA, VADODARA

Page 4: C++ 2

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 JAISWALLecturer, computer Dept.

PIETDS-2nd Shift, Limda.

PARUL INSTITUTE OF ENGINEERING & TECHNOLOGY

COMPUTER ENGG. DEPARTMENT

LIMDA, VAGHODIA, VADODARA

Page 5: C++ 2

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”

main()

{

-----

-----

-----

-----

.

.

.

----

-----

-----

Return 0;

}

Bad Development Approach

•Easer To

Design

Build

Debug

Extend

Modify

Understand

Reuse

Better Organization

Wise Development Approach

main()

{

-----

----

}

function f1()

{

---

---

}

function f2()

{

---

---

}

Page 6: C++ 2

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

Page 7: C++ 2

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.

Main

Boss

Function A Function BFunction Z

WorkerWorker

Function B2Function B1

Worker

Worker Worker Note: usual main( ) Calls other

functions, but other functions

can call each other

Page 8: C++ 2

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

Page 9: C++ 2

ADVANTAGES OF USING FUNCTIONS

4. While working on one function, you can focus onjust that part of the program construct it,

debug it,

perfect it.

5. Different people can work on different functionssimultaneously.

6. If a function is needed in more than one place in aprogram, or in different programs, you can write itonce and use it many times

9

Page 10: C++ 2

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

Page 11: C++ 2

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

Page 12: C++ 2

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

Page 13: C++ 2

EXAMPLES OF LEGAL AND ILLEGAL

USE OF FUNCTIONS WITH DEFAULT

PARAMETERS

13

Page 14: C++ 2

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

Page 15: C++ 2

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

Page 16: C++ 2

THREE OVERLOADED FUNCTIONS

THAT PERFORM SIMILAR TASKS

16

Page 17: C++ 2

POSTSCRIPT

"The important thing is not to stop

questioning; never lose a holy curiosity."

-- Albert

Einstein

Page 18: C++ 2