Save
COMPUTER PROGRAMMING 2
UNIT 1. Basic Syntax and Structure
Lesson 1.3 C++ Functions
Save
Share
Learn
Content
Leaderboard
Learn
Created by
CHRISTINE JOY
Visit profile
Cards (20)
A
function
in
C++
is a block of code that performs a
specific
task.
In order to use library functions, we usually need to include the
header file
in which these library functions are defined.
Standard Library Functions
in C++ are
predefined
and can be accessed without creating a
new function.
User-defined
Functions in C++ are created by
users.
When a
function
is invoked from any part of the program, it executes the
codes
defined in the
body
of the function.
A
parameter
is a
value
that is passed when declaring a
function.
The
return statement
in C++ can be used to return a
value
from a function.
Built-in functions
in
C++ programming
can be
accessed
by
invoking
them
directly
,
without
needing to write the
functions themselves.
The
greet()
function can be called by invoking it.
The function
add(int a, int b)
in C++ returns the
sum
of a
and b
when it is invoked.
In the function
printNum(int num)
, the int variable
num
is the function
parameter.
When a function is called, a
value
is passed to the function
parameter.
Some common library functions in C++ are
sqrt
(),
abs
(),
isdigit
(), etc.
The syntax to declare a function in C++ is:
returnType functionName
(
parameter1
,
parameter2
,
...
) { //
function body
}.
An example of a function declaration in C++ is:
void greet()
{
cout
<< "
Hello World
"; }.
The name of the function in C++ is
greet
().
The
return type
of the function in C++ is
void.
The
empty parentheses
in a function declaration in C++ mean it doesn't have any
parameters.
The function
body
in C++ is written inside
{
}.
To call a function in C++, the function
declaration
is written as:
void greet()
{
cout
<< "
Hello World
"; }.