Lecture 4
Lecture 4
Paradigms in C++
CSCI 3142
By
Priyanka Samanta
Function
Three things are needed for using C++ functions:
• Providing a function definition
• Providing a function prototype
• Calling the function
•In this case, the value of the argument variable side is passed to the
parameter variable x inside the body of function cube()cube(). Literally x
makes a copy of side
•Any changes made to x inside cube() will not have any effect on side of the
caller.
•The parameter variable x , as well as variables defined with a function, is
known as local/automatic variables. They are private to the function and
created/destroyed with the function calling
Function Arguments and Passing by Reference
In certain cases, though, it may be useful to
access an external variable from within a
function. To do that, arguments can be passed
by reference, instead of by value.
Code:
https://replit.com/@SamantaPriyanka/Function
s#passByReference.cpp
Function Arguments and Passing by Address
Passing by Pointer: Here, the memory location of the variables is passed to
the parameters in the function, and then the operations are performed.
Code: https://replit.com/@SamantaPriyanka/Functions#passByAddress.cpp
Efficiency considerations and const
references
Functions with reference parameters are generally perceived as functions that modify the
arguments passed, because that is why reference parameters are actually for.
The solution is for the function to guarantee that its reference parameters are not going to
be modified by this function. This can be done by qualifying the parameters as constant:
Inline function
Calling a function generally causes a certain overhead (stacking arguments,
jumps, etc...), and thus for very short functions, it may be more efficient to
simply insert the code of the function where it is called, instead of performing
the process of formally calling a function.
Default values in parameters
In C++, functions can also have optional parameters, for which no arguments are
required in the call, in such a way that, for example, a function with three parameters
may be called with only two. For this, the function shall include a default value for its
last parameter, which is used by the function when called with fewer arguments.
Code: https://replit.com/@SamantaPriyanka/Functions#defaultParameter.cpp
Array argument
Multiple values from function
Code link:
https://replit.com/@SamantaPriyanka/Functions#returnMultipleValues.cpp
Returning a pointer
Code link:
1. https://replit.com/@SamantaPriyanka/Functions#returnPointer.cpp
2. https://replit.com/@SamantaPriyanka/Functions#returnPointerFixed.cpp
3. https://replit.com/@SamantaPriyanka/Functions#returnPointerGlobal.cpp
Returning a Reference?
• You can’t return a reference to something created inside a function
• The object will be destroyed once the function returns, rendering the returned reference invalid
Similarly, don’t return a pointer pointing to an object being destroyed when it goes out of scope after the
function finishes
Pointer to a function
• Function pointer stores the address of a function
• Function pointer is used to achieve runtime polymorphism using function
overriding.