C++: a beginner's Guide, Second Edition



Download 11,33 Mb.
Pdf ko'rish
bet82/194
Sana12.03.2022
Hajmi11,33 Mb.
#491693
1   ...   78   79   80   81   82   83   84   85   ...   194
Bog'liq
C A Beginner\'s Guide 2nd Edition (2003)

1.
 
What are the two parameters to main( ) usually called? Explain what each contains.
2.
 
What is always the first command-line argument?
3.
 
A numeric command-line argument is passed as string. True or false?
CRITICAL SKILL 5.11: Function Prototypes
Function prototypes were discussed briefly at the beginning of this module. Now it is time to explain 
them more fully. In C++, all functions must be declared before they are used. Typically, this is 
accomplished by use of a function prototype. Prototypes specify three things about a function:

Its return type

The type of its parameters

The number of its parameters

Prototypes allow the compiler to perform three important operations:


30 
C++ A Beginner’s Guide by Herbert Schildt 

They tell the compiler what type of code to generate when a function is called. Different return 
types must be handled differently by the compiler.

They allow C++ to find and report any illegal type conversions between the type of arguments used 
to call a function and the type definition of its parameters.

They allow the compiler to detect differences between the number of arguments used to call a 
function and the number of parameters in the function.
The general form of a function prototype is shown here. It is the same as a function definition, except 
that no body is present.
type func-name(type parm_name1, type parm_name2,..., type parm_nameN);
The use of parameter names in a prototype is optional. However, their use does let the compiler identify 
any type mismatches by name when an error occurs, so it is a good idea to include them.
To better understand the usefulness of function prototypes, consider the following program. If you try 
to compile it, an error message will be issued, because the program attempts to call sqr_it( ) with an 
integer argument instead of the integer pointer required. (There is no automatic conversion from 
integer to pointer.)
It is possible for a function definition to also serve as its prototype if the definition occurs prior to the 
function’s first use in the program. For example, this is a valid program:


31 
C++ A Beginner’s Guide by Herbert Schildt 
Here, the function isEven( ) is defined before it is used in main( ). Thus, its definition can also serve as its 
prototype, and no separate prototype is needed.
In general, it is usually easier and better to simply declare a prototype for each function used by a 
program rather than trying to make sure that each function is defined before it is used. This is especially 
true for large programs in which it is hard to keep track of which functions use what other functions. 
Furthermore, it is possible to have two functions that call each other. In this case, prototypes must be 
used.
Headers Contain Prototypes
Earlier in this book, you were introduced to the standard C++ headers. You have learned that these 
headers contain information needed by your programs. While this partial explanation is true, it does not 
tell the whole story. C++’s headers contain the prototypes for the functions in the standard library. 
(They also contain various values and definitions used by those functions.) Like functions that you write, 
the standard library functions must be prototyped before they are used. For this reason, any program 
that uses a library function must also include the header containing the prototype of that function. To 
find out which header a library function requires, look in your compiler’s library documentation. Along 
with a description of each function, you will find the name of the header that must be included in order 
to use that function.

Download 11,33 Mb.

Do'stlaringiz bilan baham:
1   ...   78   79   80   81   82   83   84   85   ...   194




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish