This document discusses functions in C/C++ programming. It explains that functions allow complex problems to be broken down into smaller parts or tasks. Functions separate the concept or logic of a program from its implementation. They make programs easier to understand and allow code to be reused by calling the same function multiple times. The document also covers function definitions, prototypes, parameters, arguments, return types and provides examples of built-in library functions.
2. 2
2
P r e s e n t e d
B y,
C E N 6 8 B 1
G r o u p - 3
• Pranta Biswas
• Md. Saiful Hasan Bhuiyan
• Pallob Sikdar
• Rajib Roy
• Md. Lekhon Miah
• Parvez Ahmed
3. 3
S
F U N C T I O N
Add a Footer 3
A complex problem is often easier to solve by dividing it into
severalsmallerparts,eachofwhichcanbesolvedbyitself.
Thisiscalledstructured programming.
Thesepartsaresometimesmadeintofunctions inC/C++.
main()thenusesthesefunctions tosolvetheoriginalproblem.
4. 4
Add a Footer 4
F U N C T I O N
D E F I N I T I O N
The function definition can be placed anywhere
intheprogramafterthefunction prototypes.
Ifafunctiondefinitionisplacedinfrontofmain(),
there is no need to include its function
prototype.
5. 5
Add a Footer 5
F U N C T I O N
D E F I N I T I O N
A function definition has the following syntax:
<type> <function name>(<parameter list>){
<local declarations>
<sequence of statements>
}
For example: Definition of a function that computes the
absolute value of an integer:
int absolute(int x){
if (x >= 0) return x;
else return -x;
}
6. 6
S
Add a Footer 6
Functions separate the concept (what is done) from the
implementation(howitisdone).
Functionsmakeprogramseasierto understand.
Functions can be called several times in the same program,
allowingthecodetobereused.
A D V A N TA G E O F
F U N C T I O N
7. 7
S
Add a Footer 7
C++ allows the use of both internal (user-defined) and external
functions.
Functionsmakeprogramseasierto understand.
External functions (e.g., abs, ceil, rand, sqrt, etc.)
are usually grouped into specialized libraries (e.g.,
iostream, stdlib, math, etc.)
F U N C T I O N I N C / C + +
8. 8
S
Add a Footer 8
C++programsusually havethefollowing form:
U S E R - D E F I N E D
F U N C T I O N S
// include statements
// function prototypes
// main() function
// function definitions
9. 9
Add a Footer 9
C++programsusuallyhavethefollowing form:
E X T E R N A L F U N C T I O N S
Function Description
acos(x) Inverse cosine of x (in radians)
Asin(x) Inverse sine of x (in radians)
Atan(x) Inverse tangent of x (in radians)
Sin(x) Sine of x (in radians)
Cos(x) Cosine of x (in radians)
Tan(x) Tangent of x (in radians)
*Ceil(x) Ceiling of x (rounds up)
Exp(x) Exponential of x (base e)
Fabs(x) Absolute value of x
*Floor(x) Floor of x (rounds down)
Log(x) Natural logarithm of x (base e)
*Pow(x,p) X to the power p
Sqrt(x) Square root of x
10. 10
10
F U N C T I O N
I N P U T &
O U T P U T
RESULT
PARAMETERS
11. 11
Add a Footer 11
The function prototype declares the input and output
parametersofthefunction.
F U N C T I O N
P R O T O T Y P E
Thefunctionprototype hasthefollowing syntax:
<type> <function name>(<type list>);
Example: A function that returns the absolute value of an
integer is:
int absolute(int);
12. 12
ARGUMENTS/
PARAMETERS
Add a Footer 12
one-to-one correspondence between the arguments
in a function call and the parameters in the function
definition.
int argument1;
double argument2;
// function call (in another function, such as main)
result = thefunctionname(argument1, argument2);
// function definition
int thefunctionname(int parameter1, double parameter2)
{
// Now the function can use the two parameters
// parameter1 = argument 1, parameter2 = argument2
13. 13
Add a Footer 13
Afunctioncallhasthefollowing syntax:
<function name>(<argument list>)
Example: int distance = absolute(-5);
F U N C T I O N C A L L
Theresultofafunctioncallisavalueoftype<type>
16. 16
Add a Footer 16
C O N C L U T I O N
• Our program can determine the Diameter and Cross sectional Area of
aReinforcementBar fromanyBarNumber.
• This program is useful during designing a structure when only a Bar
Numberisgiven.
• Thisprogramisalsocanbeuseinestimatingofreinforcement.
• It is essential for people in the shop and in the field to be able to
identifyataglancethesizesofthebars.