Vous êtes sur la page 1sur 5

C++

Chapter 2: Functions in C++

-----------------------------------------------------------------------Chapter Contents About main() Function Function Prototype Function Call By Value Function Call By Passing Address Function Call By Reference Inline Function Default Arguments Function O erloading

-----------------------------------------------------------------------About main function In C!!" default return type of main function is int# In C!!" if a main function is not prefi$ed by any return type t%en it is assumed as int# In suc% cases if you do not return any int alue by using &ey'ord return" a 'arning occurs during compilation( )Function s%ould return a alue*# +%is situation can be a oided by using return &ey'ord in main or use oid before main# Function Prototype +%e prototype describes t%e function interface to t%e compiler by gi ing details suc% as t%e number and type of arguments and type of return alues# Its synta$ is return type function name (argument list); Function prototype is also &no'n as function declaration# If a function is declared outside any function t%at is globally" t%en function can be called from any part of t%e program# If a function is declared inside a particular function" t%en it is called local declaration# +%is means function can be only called from t%e function '%ere it is declared# Function Call by Value A function call by passing alues +%e called function creates a ne' set of ariables and copies t%e alues of arguments into t%em# +%is mec%anism is good if t%e function does not need to alter t%e alues of t%e original ariables in t%e calling program#

#include<iostream.h ,,%eader file contains declaration of cout and cin int area!int"int#$ ,, function declaration,prototype %oid main!# & int l" b" a$ cout<<'(nter len)th and breadth*$ cin l b$ a+area! l " b#$ ,,function area() is called by passing t'o int alues cout<<'Area of rectan)le is '<<a$ , int area!int -" int .# ,,function definition & return!-/.#$ , -ere function area() is called by passing t'o int alues" t%at is mentioned by 'riting ariable names separated by comma in parent%esis# Values of l and b are copied into ariables . and B respecti ely# Function can only access its o'n ariables" t%erefore function area() cannot access l and b# +%is is t%e reason 'e copy alue of l and b in . and B# Function area() returns a alue produced by product of . and B# +%is alue is t%en stored in ariable /a0 of function main()# Function call by passin) address +%ere may some scenarios '%ere 'e 'ould li&e to c%ange t%e alues of ariables in t%e calling program# Addresses can al'ays be stored in special ariables called pointers# #include<iostream.h main!# & int a"b$ %oid s0ap!int /" int /#$ cout<<'(nter t0o numbers: '$ cin a b$ s0ap!1a"1b#$ cout<<'a+'<<a<<* b+'<<b$ , %oid s0ap!int /p" int /2# & int t$ t+/p$ /p+/2$ /2+t$ ,

Abo e program is of s'apping t%e contents of t'o ariables /a0 and /b0# 1otice t%e function call s0ap!1a" 1b#" %ere 'e pass addresses of ariables /a0 and /b0 in place of alues as 'e 'ant to ma&e c%anges possible in ariable /a0 and /b0 from function s'ap()# +%ese addresses get collected in pointers p and 2# s'ap () function returns not%ing as it can directly ma&es c%anges in ariables a and b using pointers p and 2# Function call by reference A reference is implicit pointers t%at for all intents and purpose act li&e anot%er name for a ariable# C!! permits us to pass parameters to t%e functions by reference# +%e formal arguments in t%e called function become aliases (alternati e name) to t%e actual arguments# #include<iostream.h %oid main!# & int a"b$ %oid s0ap!int 1" int 1#$ cout<<'(nter t0o numbers: '$ cin a b$ s0ap!a"b#$ cout<<'a+'<<a<<* b+'<<b$ , %oid s0ap!int 13" int 1y# & int t$ t+3$ 3+y$ y+t$ , -ere" function s'ap() is called by passing references of ariable /a0 and /b0# $ and y are reference ariables t%at is anot%er names of /a0 and /b0 respecti ely 3o 'e do not create e$tra copies of /a0 and /b0# 4nline function Function in a program is to sa e memory space '%ic% becomes appreciable '%en a function is li&ely to be called many times# -o'e er e ery time a function is called" it ta&es lot of e$tra time in e$ecuting a series of instructions for tas&s suc% as 4umping to t%e functions" sa ing registers" pus%ing arguments into t%e stac& and returning to t%e calling function# 3o '%en function is small it is 'ort%less to spend so muc% e$tra time in suc% tas&s in cost of sa ing comparati ely small space#

+o eliminate t%e cost of calls to small functions" C!! proposes a ne' feature called inline function# An inline function is a function t%at is e$panded in line '%en it is in o&ed# Compiler replaces t%e function call 'it% t%e corresponding function code# inline is a re2uest not a command# +%e benefit of speed of inline functions reduces as t%e function gro's in si5e# 3o t%e compiler may ignore t%e re2uest in some situations# Fe' of t%em6 Function containing loops" s'itc%" goto# Functions 'it% recursion Containing static ariable# #include<iostream.h inline int add!int" int#$ %oid main!# & int a"b$ cout<<*(nter t0o numbers*$ cin a b$ int sum+add!a" b#$ cout<<*5um is '<<sum$ , int add!int 3" int y# & return!3+y#$ , 6efault ar)uments C!! allo's us to call a function 'it%out specifying all its arguments# In suc% case function assigns a default alue to t%e parameter '%ic% does not %a e a matc%ing argument in t%e function call# Default alues are specified '%en t%e function is declared# #include<iostream.h int add!int"int"int 7+8#$ %oid main!# & int a"b"c$ cout<<*(nter three numbers*$ cin a b c$ int s9+add!a"b"c#$ cout<<s9$ int s2+add!a"b#$ cout<<s2$ , int add!int 3"int y"int 7#

& return!3+y+7#$ , +%e default alue is specified in a manner syntactically similar to a ariable initiali5ation# +%e abo e prototype declares a default alue of 7 to t%e argument 5# -ere" 'e %a e only one ersion of function add" '%ic% is capable of ta&ing t%ree arguments as 'ell as t'o arguments# 8%en function add() %as t'o parameters" t%en first alue is recei ed in $" second alue is recei ed in y and t%ere is no alue for 5 but 5 is by default containing 7# Function o%erloadin) O erloading refers to t%e use of same t%ing for different purposes# Function o erloading means" 'e can use t%e same function name to create functions t%at perform a ariety of different tas&s# +%is is called function polymorp%ism or compile(time polymorp%ism in OOP# Anot%er is run time polymorp%ism '%ic% 'e0ll discuss later +%e correct function to be in o&ed is determined by c%ec&ing t%e number and type of t%e arguments but not on t%e function type (return type)# #include<iostream.h float area!int#$ int area!int"int#$ %oid main!# & int r$ cout<<*(nter radius of a circle*$ cin r$ float a+area!r#$ cout<<*Area of circle is '<<a$ int l"b"A$ cout<<*(nter len)th and breadth of rectan)le*$ cin l b$ A+area!l"b#$ cout<<*Area of rectan)le is '<<A$ , float area!int :# & return!9.;</:/:#$ , int area!int -" int .# & return!-/.#$ ,

Vous aimerez peut-être aussi