You are on page 1of 3

Lab Manual

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:

Figure 1- Function Declaration in C++

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

Figure 2-Calling a Function in main program

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.

Figure 3- Function with multiple inputs

Functions Return Statements


The return statement of any function can be any of the datatypes that have been studied previously
including Float , Integer , String or boolean. Void keyword indicates that the function is not going to
return any data variable. An example of a function that returns some value can be a function that does
some calculation on the arguments passed for result. A function can only return one value.

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

Figure 4- Declaring two or more functions with same name

Generic Functions and Template


The concept of generics enable us to use same functions serving similar functions but with different
data types.Templates are created to make use of same function on multiple data types.An example
of this is shown below:

Figure 5- Generic Function and Templates

Recursion and Recursive functions


Recursion is a process where a function invokes itself or a function is called within itself.

Figure 6- Recursive Function Example

SZABIST KARACHI

You might also like