Professional Documents
Culture Documents
Experiment 1
Introduction to Functional Programming
OBJECTIVE
Introduce Functions and their Arguments
Declaring Function in C++(Visual Studio)
Function return statements
Function Overloading
Generic Functions and Templates
Recursion and recursive functions
THEORY
Introduction to Functions
A function is a block of code that is grouped together that usually serves a specific purpose. The
function serves that purpose only when it is called into the main program. The lines of code in the
function statement will only be executed when the function is invoked into the program. An
example of function is the void main() Function. The main benefit of using functions in building a
program is that it makes code reusable with utmost convenience. A function can be invoked
multiple times serving the exact same purpose.
Definition of Function
The first thing that needs to be done in declaring any function is to state the return type of the
function. For the moment consider this as void , which means that this function is not returning
anything. After the return datatype, the name of the function is written followed by
parentheses(round brackets). Curly brackets({ , }) are placed after all of these statements in which
the line of codes are written. An example of the above mentioned explanation is given below:
Declaring a function does not mean that is going to be executed in the main program. The block of
code in the function or the purpose of function is only going to be executed once the function is called
into the main program.An example of invoking the function in the main program is also detailed
below:
SZABIST KARACHI
Lab Manual
Arguments(Parameters, Inputs)
Argument or parameter is an input that is needed by a function to perform a certain task. This is done
by first defining the datatype of the parameter and then mentioning the name of the variable. All of this
is done in the round brackets.A single function can have multiple inputs or parameters serving different
purpose. Multiple inputs are separated by a single comma. A function with multiple inputs must be
presented with all the inputs for correct functioning.
Function Overloading
Function overloading is a feature of object oriented programming where two or more functions can
have the same name but different parameters.
SZABIST KARACHI
Lab Manual
SZABIST KARACHI