19
www.sahajsolns.com Chapter 3 Chapter 3 OOPS WITH C++ 1 Sahaj Computer Solutions

Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Chapter 3Chapter 3

OOPS WITH C++ 1Sahaj Computer Solutions

Page 2: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Session Objectives� Simple Functions

� Function declaration

� Calling the functions

Function definition� Function definition

� Passing arguments to function

� Returning values from function

� Passing Constants

� Pass by Value

OOPS WITH C++ Sahaj Computer Solutions 2

Page 3: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Session Objectives� Passing Structure variables

� Pass by Reference

� Overloaded Functions

Different number of Arguments� Different number of Arguments

� Different kind of Arguments

� Inline Functions

OOPS WITH C++ Sahaj Computer Solutions 3

Page 4: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Introduction� Functions play an very important role :

� Dividing the programs into functions is one of the major principles of top down approach.

� Another advantage of functions is that it reduces size of � Another advantage of functions is that it reduces size of the program.

� The syntax for declaring a function is as same in C:

return-type function-name(variable-list)

{

//statements

return (value);

}

OOPS WITH C++ Sahaj Computer Solutions 4

Page 5: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Simple Functions#include<iostream.h>

//Function Declaration

void starline();

void main()

//Function Definition

void starline()

{

for(int i=0;i<45;i++)void main()

{

//call to function

starline();

}

for(int i=0;i<45;i++)

cout<<“*”<<endl;

}

OOPS WITH C++ Sahaj Computer Solutions 5

Page 6: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Function Declaration (Prototype)� Just like variable functions are to be declared in the

beginning of the program

� In the above example void starline();

Was declared above the main function.

� The declaration tells the compiler that at some later point we plan to present a function called starline.

� The keyword void specifies that the function is not returning any value.

� Function declarations are also called prototype, since they provide a model or a blue print for the function.

OOPS WITH C++ Sahaj Computer Solutions 6

Page 7: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Calling the Function� To execute any function we need to call it.

� In above example starline() was called from main function which tells the compiler to transfer the control to the function starline().control to the function starline().

� To call a function we need : the function name, followed by parentheses .

� When a function is called the control leaves from calling function to the called function and after executing it comes back to the calling function.

OOPS WITH C++ Sahaj Computer Solutions 7

Page 8: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Function Definition� The function itself is known as the function definition.

� The definition contains the actual code for the function.

� Ex:

void starline() // declaratorvoid starline() // declarator

{

for( int i=0;i<45;i++ ) //function body

cout<<“*”;

}

� The definition consists of a declarator, followed by the function body.

OOPS WITH C++ Sahaj Computer Solutions 8

Page 9: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Function Definition� The body consists of statements that make up a

function.

� The declarator must agree with the declaration:

� It must have same function name � It must have same function name

� Same number of arguments types in same order

� And have same return type

OOPS WITH C++ Sahaj Computer Solutions 9

Page 10: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Passing Arguments to functions� Passing Constants

� Program: CH3PG2.CPP

� Passing Variable

Program: CH3PG3.CPP� Program: CH3PG3.CPP

OOPS WITH C++ Sahaj Computer Solutions 10

Page 11: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Returning Values from Function� When a function completes its execution it can return

a single value to the calling function.

� Usually this return value consists of an answer to the problem the function has solved.problem the function has solved.

� The below program demonstrates returning values from function.

� Program: ch3pg4.cpp

OOPS WITH C++ Sahaj Computer Solutions 11

Page 12: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Passing Constants� In C++ an argument to a function can be declared as

const

� For example:

int strlen(const char *p);int strlen(const char *p);

Int length(const string &s);

� The qualifier const tells the compiler that the function should not modify the argument.

� The compiler will generate an error when this condition is voilated.

OOPS WITH C++ Sahaj Computer Solutions 12

Page 13: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Pass by Value� There are two parameter passing techniques:

� Pass by Value

� Pass by reference

� Pass by Value: In this the calling function sends the � Pass by Value: In this the calling function sends the copy of actual parameters to the called function.

� So any modification done to the variable in called function is not reflected in the calling function.

� Example:� Program CH3PG9.cpp

OOPS WITH C++ Sahaj Computer Solutions 13

Page 14: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Pass by reference� In pass by value the called function creates a new variable

of the same type as the argument and copies the argument values into it.

� It is useful when you don’t want to change the original values.values.

� In some cases we need to access the actual variables in the calling function.

� In this case pass by reference technique can be used which sends the address of actual variables to the calling function.

� Any change in called function will be reflected on the actual parameters. PROGRAM: ch3pg6.cpp

OOPS WITH C++ Sahaj Computer Solutions 14

Page 15: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Inline Functions� A function’s objective is to save memory space.

� But whenever a function call is made it takes extra time in executing a series of instructions for tasks such as jumping to the function, saving registers, such as jumping to the function, saving registers, pushing arguments into stack and returning values to the calling function.

� When functions are small, a substantial percentage of execution time may be spent in such overheads.

� One solution to such a problem is C++’s inline function.

OOPS WITH C++ Sahaj Computer Solutions 15

Page 16: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Inline function� An inline function is a function that is expanded in

line when it is invoked.

� That is , the compiler, replaces the function call with the corresponding function code.the corresponding function code.

� Syntax:

inline data-type function-name(arguments)

{

function body;

}

� Program: ch3pg7.cpp

OOPS WITH C++ Sahaj Computer Solutions 16

Page 17: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Default Arguments� C++ allows a function to be called without specifying

all its arguments.

� This can be achieved with default arguments which must be declared in the function prototype.must be declared in the function prototype.

� The default values must be provided for those arguments that are not specified.

� Whenever the function finds the missing arguments it takes the default arguments from the declaration.

� This is useful when a function is overridden.

� Program: ch3pg8.cpp

OOPS WITH C++ Sahaj Computer Solutions 17

Page 18: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Default Arguments� Default arguments are checked for type at the time of

declaration and evaluated at time of function call.

� One important point to note is that only the trailing arguments can have default values and therefore we must add defaults from right to left.add defaults from right to left.

� We cannot provide default arguments in between of any argument list.int mul(int i, int j=5, int k=10) //legal

int mul(int j=5, int k) //illegal

int mul(int i=0, int j, int k=10) //illegal

int mul(int i=2, int j=5, int k=10) //legal

OOPS WITH C++ Sahaj Computer Solutions 18

Page 19: Chapter 3 · Function declaration Calling the functions Function definition Passing arguments to function Returning values from function Passing Constants Pass by Value OOPS WITH

www.sahajsolns.com

Sahaj Computer Solutions Near Gomatesh School, Hindwadi Belgaum-11, Phone no: 4200864,2481703

Get Certified in

•Microsoft Office 2010•Tally 9.2•.NET 2008•J2EE•CORE JAVA

� Around 1800 students developed software projects and scored the top scores in exams.

� One stop shop for •CORE JAVA•C PROGRAMMING•OOPS WITH C++•JOOMLA•WORDPRESS•PHP AND MYSQL•SQLSERVER•UNIX AND LINUX•ANDRIOD APPS

� One stop shop for � Software Projects

� Website Development

� Technology Training

� I.T Research Visit: www.sahajsolns.com

OOPS with C++ Sahaj Computer Solutions 19