Week 8
Week 8
FUNDAMENTALS OF
PROGRAMMING
Lecture 8: FUNCTIONS
Output = f(a,b)
Motivation for using Functions
The divide-and-conquer approach
Itmakes program development more manageable by
constructing programs from small, simple pieces.
Software reusability
Usingexisting functions as building blocks to create new
programs.
Avoids repeating code.
Dividing a program into meaningful functions makes
the program easier to debug and maintain.
Functions
Every function should be limited to performing a
single, well-defined task
E.g sum and difference operations should make 2
functions, not one.
Name of the function should be sensible. Such
functions make programs easier to write, test,
debug and maintain.
Callingyour functions “func” or “A” will make your
program listing difficult to understand and follow.
Functions
Any portion of the code that carries out a specific
task or is to be repeated several times can be
converted into a function.
For example, the summing operation takes 2 inputs
and returns their sum as output.
sum() function can be called every time to calculate the
sum
Pre-Defined Functions
Programs written by
combining new functions with “prepackaged”
functions in the C++ standard library.
The standard library provides a rich collection of
functions.
Input/output(getline, read etc.)
Common mathematical calculations (pow, sqrt, exp, etc.)
String manipulations(strcpy, strcat, etc.)
Many more
6
Function Call
7
Function Definitions
Function definitions
Only written once
These statements are hidden from other functions.
8
Math Library Functions
9
Math Library Functions
10
Components of a Function
Prototype or Function Declaration
Function Call
Function Definition or Function Body
Preprocesssor lines
int main( )
{
call function1(); //function call
call function2();
……
}
int function2( )
{
……
Functions
Function Prototype ( Declaration)
Tells
the compiler that a function of this kind is defined
somewhere in the program
Function Call
Takes the program control to the called function
Function Header and Body (Definition)
Containsthe body of the function i.e. all the commands
that make up the function
Example
#include <iostream>
using namespace std;
void sum(void); Declaration
int main()
{
sum(); Function Call
return 0;
}
void sum(void) Definition or Body
{
float first, second;
cin >> first >>second;
cout << first + second;
}
Types of Functions
Functions with no input and no output
Functions with input but no output
Functions with no input but output
Functions with input and output
Functions
Function
int main()
{
int x, a=0;
while(a<10)
{
cout << "press 7";
cin >> x;
if(x!=7)
errormessage(); // function call
a++;
}
return 0;
}
Function
void function (int)
int main(void)
{
float x, y;
int num;
x = 4.0;
y=1.0;
square(y, num); //function call
return 0;
}
Function
int function (void)
Function
int function (int)
int main()
{
int a,b,c;
Actual Parameters
a = 5;
b = 7;
c = our_formula(a,b); // function call
cout <<"result = “ << c;
}
int func(void)
{ ddd dd
cin>>a;
cin>>b;
………..
return a;
}dddddddddd
Function Signatures
Portion of function prototype that includes the name
of the function and the types of its arguments is
called the function signature.
Function in the same scope must have unique
signatures.
Variables and Storage Classes
The storage class of a variable determines which
parts of the program can access it (scope)and how
long it stays in existence
(lifetime)
Types of variables
Automatic or Local
Global or External Variable
Static
Scope and Life Time of Variable
Life Time
The time b/w creation and destruction of variable is
called the life time or duration. Lifetime of a variable is
limited to save memory.
Scope
Scope refers to blocks of code within which variables
are visible. Scope limits visibility to improve
organization and modularity of the program.
Scope
The life-time of variables exists within the variable’s
scope.
After declaration, a variable can be referenced
anywhere within its scope.
Every set of braces has its own scope, and can contain
local variables.
The moment the set of braces in which a variable was
declared ends, the variable goes out of scope, i.e. it
can no longer be referenced as an identifier.
Scope
The program erases variables that have gone out of
scope from memory, i.e. their life-time ends.
The scope of arguments to a function is the entire
function body.
No two variables may share the same name within a
scope.
Scope
Example: A variable declared in the first line of a
function can be used anywhere in the function, but
nowhere outside of it.
The moment the function exits, the variable ceases to
exist in memory, i.e. its scope and lifetime end.
Automatic or Local Variables
Variables defined within the function body (or a set
of braces) are called automatic or local variables.
Properties
It is most commonly used.
It is not created until the function in which it is defined is
called.
Automatic or Local Variables
Life Time
When control is returned to the calling program, these
variables are lost. Life time is equal to the life of
function.
Scope
Automatic variable are only visible with in function, they
can only be accessed within the function in which they
are created.
Example (Local Variables)
#include <math.h>
void squareroot(float);
int main(void)
{
float x;
x = 4.0;
squareroot(x); //function call
}
int main(void)
{
::first = 23.45;
::second = 87.555;
cout <<"sum = “ << sum();
return 0;
}
float sum()
{
return ::first + ::second;
}
Static Local Variables
Static variable remembers and maintains its value
even after its scope ends.
Static Local variables are used when it is necessary
for a function to remember a value between call to
other functions.
For example, variable count used in a func
Static Local Variables
Scope
Static variable is visible only inside the function in which
it is defined.
It has the same scope as local variables.
Life time
Lifeof a static variable is the same as that of a global
variable, i.e. it remains in existence for the life of the
program.
Example – Static Variables
void function(void);
int main()
{
cout << "Count the number of times the function is
called : ";
for (int i=0; i<5; i++)
function();
}