0% found this document useful (0 votes)
3 views23 pages

C Functions

The document explains C++ functions, which are blocks of code that execute when called and can accept parameters for data input. It highlights the benefits of user-defined functions, such as code reusability, improved organization, and enhanced readability. Additionally, it covers function declaration, definition, parameters, arguments, default parameter values, and return values.

Uploaded by

1801434
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

C Functions

The document explains C++ functions, which are blocks of code that execute when called and can accept parameters for data input. It highlights the benefits of user-defined functions, such as code reusability, improved organization, and enhanced readability. Additionally, it covers function declaration, definition, parameters, arguments, default parameter values, and return values.

Uploaded by

1801434
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

C++ Functions

What is a function?

A function is a block of code which only


runs when it is called.
 You can pass data, known as
parameters, into a function.
 Functions are used to perform certain
actions, and they are important for
reusing code: Define the code once, and
use it many times.
Benefits of Using
User-Defined Functions
 Functions make the code reusable.
We can declare them once and use
them multiple times.
 Functions make the program easier
as each small task is divided into a
function.
 Functions increase readability.
User-Defined Functions
 Return Type − A function may return a
value. The return_type is the data type of
the value the function returns. Some
functions perform the desired operations
without returning a value. In this case, the
return_type is the keyword void.
 Function Name − This is the actual name
of the function. The function name and the
parameter list together constitute the
function signature.
 Parameters − A parameter is like a
placeholder. When a function is invoked, you
pass a value to the parameter. This value is
referred to as actual parameter or
argument. The parameter list refers to the
type, order, and number of the parameters
of a function. Parameters are optional; that
is, a function may contain no parameters.
 Function Body − The function body
contains a collection of statements that
define what the function does.
Create a Function
 C++ provides some pre-
defined functions, such
as main(), which is used to
execute code.

 myFunction() is the name of


the function
 void means that the function
does not have a return
value.
 inside the function (the
body), add code that defines
what the function should do
Call a Function
 Declared functions are
not executed
immediately. They are
"saved for later use", and
will be executed later,
when they are called.
 To call a function, write
the function's name
followed by two
parentheses () and a
semicolon ;
 A function can be called
multiple times
Call a Function
Function Declaration and
Definition
 A C++ function consist of two parts:
 Declaration: the return type, the
name of the function, and
parameters (if any)
 Definition: the body of the function
(code to be executed)
Function Declaration and
Definition
 Ifa user-defined function, such
as myFunction() is declared after
the main() function, an error will
occur
Function Declaration and
Definition
 However, it is possible to
separate the declaration
and the definition of the
function
 You will often see C++
programs that have
function declaration
above main(), and function
definition below main().
 This will make the code
better organized and
easier to read.
Parameters and Arguments
 Information can be passed to functions as
a parameter. Parameters act as variables
inside the function.
 Parameters are specified after the
function name, inside the parentheses.
You can add as many parameters as you
want, just separate them with a comma.
Parameters and
Arguments
 When
a parameter is
passed to the
function, it is
called
an argument.
 fname is
a parameter,
while Liam, Jenn
y and Anja are
arguments.
Parameters and
Arguments
Default Parameter Value
 You can also
use a default
parameter
value, by using
the equals sign
(=).
 If we call the
function without
an argument, it
uses the default
value
Working of default arguments
Working of default arguments
Default Argument
Multiple Parameters

 Note that when you are working with multiple parameters, the
function call must have the same number of arguments as there are
parameters, and the arguments must be passed in the same order.
Return Values
 The void keyword
indicates that the
function should not
return a value.
 If you want the
function to return a
value, you can use a
data type (such
as int, string, etc.)
instead of void, and
use the return keyword
inside the function
Return Values

You can also store the


result in a variable
Return values
Pass Arrays as Function
Parameters
References

 https://www.w3schools.com/cpp/cpp_
functions.asp
 https://www.programiz.com/cpp-
programming/function

You might also like