Functions
Functions
Main-program{
Int a=36, b=24;
Cout<<gcd(a,b);
Cout<<gcd(99,47);
}
Using a function
• Code has two parts: Function definition + main program
• Main program
• “calls” or “invokes” function
o gcd(a,b)- call or invocation
o gcd(99,47)-another call
• Call includes values whose gcd is to be calculated
o a,b in first call
o 99,47 in second call
• Value supplied as part of call: “arguments to the call”
General form of function definition
Return-type name-of-function(parameter1-type parameter1-
name, parameter2-type parameter2-name, ..){
Function body
}
return type: the type of value returned by the function. Eg:int
• Some functions may not return anything
• Name-of-the-function: gcd
• Parameter: variables that will be used to hold the values of
the arguments to the functions. m,n in gcd.
• Function body: code that gets executed
How a function executes
• Main program execution begins
• When control reaches the function execution of main program is suspended not
stopped
• Preparation to run sub program (function) gcd
• Area allocated in where gcd will have its variables. “activation frame”
• Variables corresponding to parameters are created in activation frame
• Values of arguments are copied from activation frame of main program to gcd. This is
termed passing “arguments by value”
• Execution of gcd starts
• Execution of gcd is familiar (discussed earlier)
• Execution of function ends when return statement is encountered.
• Value following the return is copied back to the main program where function is called.
• Here it will be used in the place of expression gcd(...,....)
• Activation frame of function is destroyed. Memory reserved for it is taken back.
• Main program resumes and prints the output
Example
int gcd (int m, int n)
{....
return n}
int lcm(int m, int n)
{return m*n/gcd(m,n);
}
main-program{
cout<<lcm(50,75);
}
Execution
• Main program starts executing
• Call lcm reached. Main program suspends.
• Activation frame created for lcm.
• 50,75 copied to lcm.lcm starts execution.
• Call to gcd encountered.lcm suspends.
• Activation frame created for gcd.
• 50,75 copied to m,n.
o Execution of gcd starts
o Gcd computes 25 as result
o Result copied to activation frame of lcm
• Activation frame of gcd destroyed
• Lcm continues execution using result
• M*n/gcd(m,n) =50*75/25 =150 computed
• Returned to main-program
• Activation frame of lcmdestroyed
Points to remember
• Function: piece of code which takes the responsibility of
getting something done.
• Specification: what the function is supposed to do
“If the argument satisfies certain properties, then a certain
value will be returned or a certain action will happen”
• Certain properties=pre conditions
• Exmple: gcd: if positive integers are given as arguments,
then their GCD will be returned.
• If preconditions are not satisfied, there will be wrong
execution
Some shortcomings
• By using passing by values it is not possible to
do the following:
• A function that exchange the value of two
variables.
• A function that produce several values as
result:
o Function to produce polar coordinates given
cartesian coordinates.
Exchanging the value between two variables
Int main(){
Cout<<N::lcm(36,24)<<lc(36,24)endl;
Cout<<N::lcm(2,4)
}
The using directive
• Suppose you refer to names defined in some
namespace N frequently
• You may find it tedious to write N:: all the time
• Put the following line at the top of your
pragram
o Using namespace N;
• Then you will be allowed to use any name
from N without having to write N:: before it.
Example using “using”
• Namespace N{
Int gcd(int m, int n){....}
Int lcm (int m , int n){...}
}
Using namespace N;
Int main(){
Cout<<lcm(36,24)<<endl;
}
Scope resolution operator
• The scope resolution operator ( :: ) is used for several reasons. For example: If the
global variable name is same as local variable name, the scope resolution operator
will be used to call the global variable.
Example:
#include <iostream>
using namespace std;
char a = 'm';
static int b = 50;
int main()
{
char a = 's'; cout << "The static variable : "<< ::b;
cout << "\nThe local variable : " << a;
cout << "\nThe global variable : " << ::a;
return 0;
}
simplecpp
• # include <simplecpp> includes the following
lines
• # include <iosream>
• #include<cmath>
• Using namespace std;
o These lines are useful . The names cout, cin, endl
are defined in the namespace std, in the
standard header file iostream.
• Using simplecpp, we will be able to do graphics