0% found this document useful (0 votes)
12 views

DR M. Arifur Rahman: PHY-206 Cpna

This document describes functions and how to use them in C++. It explains how to declare and call standard functions, and use standard classes. It provides examples of function prototypes, mathematical functions, declarations and definitions. It also discusses functions without return values or arguments, using header files, and header files of the C and C++ standard libraries.

Uploaded by

Muheb Bullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DR M. Arifur Rahman: PHY-206 Cpna

This document describes functions and how to use them in C++. It explains how to declare and call standard functions, and use standard classes. It provides examples of function prototypes, mathematical functions, declarations and definitions. It also discusses functions without return values or arguments, using header files, and header files of the C and C++ standard libraries.

Uploaded by

Muheb Bullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PHY-206

CPNA

Dr M. Arifur Rahman
Associate Professor
Content

This chapter describes how to-

 Declare and call standard functions


 use standard classes

11/17/20 A Complete Guide to Programming in C++ 2


Example of a function prototype

11/17/20 A Complete Guide to Programming in C++ 3


Examples of Mathematical functions

11/17/20 A Complete Guide to Programming in C++ 4


Declarations and Definitions of Functions
 A function declaration tells the compiler about
a function's name, return type, and parameters. A function
definition provides the actual body of the function

 Each name (identifier) occurring in a program must be known to


the compiler or it will cause an error message. That means any
names apart from keywords must be declared, i.e. introduced to
the compiler, before they are used.

 Each time a variable or a function is defined it is also declared. But


conversely, not every declaration needs to be a definition.

 If you need to use a function that has already been introduced in a


library, you must declare the function but you do not need to
redefine it.

11/17/20 A Complete Guide to Programming in C++ 5


Example

 The name and type of the function and the type of


each argument. This is also referred to as the
function prototype.

 Examples:
int toupper(int);
double pow(double, double);

int toupper(int c);


double pow(double base, double exponent);

11/17/20 A Complete Guide to Programming in C++ 6


Sample Program: Function Calls

11/17/20 A Complete Guide to Programming in C++ 7


Function Calls

 A function call is an expression of the same type as


the function and whose value corresponds to the
return value. The return value is commonly passed
to a suitable variable.
 Example:
 y = pow( x, 3.0);
 cout << 2.0 + pow( 5.0, x);

 float x = pow(3.0 + 4.7); // Error!

11/17/20 A Complete Guide to Programming in C++ 8


11/17/20 A Complete Guide to Programming in C++ 9
Functions without Return Value

You can also write functions that perform a certain


action but do not return a value to the function that
called them. The type void is available for functions of
this type, which are also referred to as procedures in
other programming languages.

 Example: void srand( unsigned int seed );

11/17/20 A Complete Guide to Programming in C++ 10


Functions without Arguments

If a function does not expect an argument, the function


prototype must be declared as void or the braces
following the function name must be left empty.

 Example: int rand( void ); // or int rand();

11/17/20 A Complete Guide to Programming in C++ 11


Using header files

11/17/20 A Complete Guide to Programming in C++ 12


Using header files

Header files are text files containing declarations and


macros. By using an #include directive these declarations
and macros can be made available to any other source file,
even in other header files.

Pay attention to the following points when using header files:


 header files should generally be included at the start of a
program before any other declarations
 you can only name one header file per #include directive
 the file name must be enclosed in angled brackets < ... > or
double quotes

11/17/20 A Complete Guide to Programming in C++ 13


Header files of the C standard library

11/17/20 A Complete Guide to Programming in C++ 14


Header files of the C++ standard library

11/17/20 A Complete Guide to Programming in C++ 15

You might also like