0% found this document useful (0 votes)
4 views16 pages

Lecture 4

The document provides an overview of using functions in C++, detailing function definitions, prototypes, and various ways to pass arguments (by value, reference, and address). It also discusses efficiency considerations, inline functions, default parameter values, and the use of pointers and void pointers. Additionally, it includes code links for practical examples and exercises for further practice.

Uploaded by

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

Lecture 4

The document provides an overview of using functions in C++, detailing function definitions, prototypes, and various ways to pass arguments (by value, reference, and address). It also discusses efficiency considerations, inline functions, default parameter values, and the use of pointers and void pointers. Additionally, it includes code links for practical examples and exercises for further practice.

Uploaded by

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

Programming

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

Code link: https://replit.com/@SamantaPriyanka/Functions#main.cpp


Function definition and prototyping
•The return value can’t be an array
•Everything else is possible: int, double, pointers, and even objects
•Function can return an array that’s part of a structure or object
•you could leave out the parameter names in function prototypes
•Function prototypes notify the compiler beforehand about the interface (types of
parameters and return value), thus make it easier to catch errors (static type
checking)
•While theoretically the compiler could look for the function definition itself, it’s not efficient.
It’s common that the function definition may appear in other files common with libraries.
Convention is to put main() first, and function definition later, so prototypes are needed.
Function Arguments and Passing by Value
C++ normally passes arguments by value

•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

The following is ok.

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.

Syntax: return_type (*fPtr)(parameter List)


Example: int(*fPtr)(int,int);
Code link:
1. https://replit.com/@SamantaPriyanka/Functions#functionPointer.cpp
2. https://replit.com/@SamantaPriyanka/Functions#functionPointer2.cpp
Void pointers
• The void type of pointer is a special type of pointer with no type. (and thus also an
undetermined length and undetermined dereferencing properties).
• Greater flexibility, by being able to point to any data type, from an integer value or a float
to a string of characters.
• limitation: the data pointed to by them cannot be directly dereferenced
• Any address in a void pointer needs to be transformed into some other pointer type that
points to a concrete data type before being dereferenced.
• One of its possible uses may be to pass generic parameters to a function
• Be careful while using void pointer !!!

Code link: https://replit.com/@SamantaPriyanka/Pointer-and-reference#void_pointer_2.cpp


https://replit.com/@SamantaPriyanka/Pointer-and-reference#void_pointer.cpp
Practice
1. Write a function using void pointer that can accept any data of type (int,
char, float, double) and add 5 to it and print the result.

2. Rewrite the getMinMax() function using pointer instead of reference.

You might also like