Session-10
Session Overview
Modular Programming -2
1
Functions Arguments
Before discussing arguments in detail, how arguments are passed to a function.
Let us see a small example in which square of numbers from 1 to 10 is calculated.
main()
{
int I;
for(i=1;i<=10;i++)
printf(“\n Square of %d is %d “, i, square(i));
}
squarer(int x)
{
int j;
j=x*x;
return(j) ;
}
2
Functions Arguments contd.
Conclusions:
• The data is passed from the calling routine (main()), to the function squarer,
which works on the passed arguments.
• The arguments are known as actual arguments in the calling routine and as
formal arguments in the called function.
• Here main( ) is the calling function, Squarer is a called function.
• The data type of the actual arguments should be the same as the formal
arguments.
• The number and order of the actual arguments should also be the same as that
in that formal arguments.
3
Functions Arguments contd.
Conclusions:
• When a function is invoked, control is passed to the called function where the
formal arguments(parameters) are replaced by actual arguments(parameters).
• The function is then executed and, on encountering the return statement,
control is transferred back to the calling program.
• The function squarer( ) is called by passing the number whose square is
required. The argument x can be represented as one of the following while
defining the function.
squarer ( int x) /* x defined with its type in parentheses */
squarer ( x )
int x;
x is in parentheses, and its type is defined immediately after the function name
4
Functions Arguments contd.
• When the arguments are declared within the parentheses, each and every
argument has to be defined individually, irrespective of whether they are of the
same type.
If x and y are two arguments of a function abc( ), then
abc ( char x, char y) is right declaration
abc ( char x, y) is erroneous {
int i=0;
Parameters are passed from left to right when prin(++i, ++i, ++i);
…
passed to a function. }
Here the parameters passed to the function are 1 2 3. prin( a, b, c)
But the output of the prin( ) function will be 3 2 1 int a, b, c;
{
and not 1 2 3. printf(“%d %d %d”, a, b, c);
}
5
Functions Return statement
• The return statement is used to return from a function.
• It causes execution to return to the point at which the call to the function was
made.
• The return statement can have a value with it, which it returns to the program.
Syntax: return (expression);
• The expression is optional.
• More than one return can be used in a function. However, the function will
return when the first return is encountered.
6
Functions Return statement contd.
• The return statement serves two purposes
It immediately transfers the control from the function back to the calling
program.
Whatever is inside the parentheses following the return is returned as a
value to the calling program.
• For a function which doesn’t return any value to the calling routine, then the
return statement can be written as return(0) or return, or ignored all together.
• If the return statement is left out, control passes to the calling program when
the closing braces of the code block are encountered. The is termed as ‘falling
of the end’.
• Writing a return at the end of the function, however, is a good programming
practice.
7
Functions Return statement contd.
• Note that if a function is supposed to return a value and it does not do that,
then it will return some garbage value.
• return statement can be used in any of the following ways:
return;
return(constant);
return(variable);
return(expression);
return(statement on evaluation);
• However, a limitation of return is that it can return only one value.
8
Functions Type of a Function
• The type-specifier is not written prior to the function previously.
• The type-specifier is not compulsory if an int type of value is returned or if no
value is returned.
• If there are many functions in a program, it is better to specify int if an integer
value is to be returned.
• If the function returns nothing, the type void is used.
• A function can be called from any point in a program by just specifying the
function’s name and the necessary arguments.
9
Functions Invoking a Function
• A function can be invoked or called from the mail program simply by using its
name, including the parentheses, which follow the name.
• The parentheses are necessary so that the compiler knows that a function and
not a variable is being referred to.
• When a function name is used in the calling program it is either part of a
statement or a statement itself so it always ends with a semi-colon.
• However, when defining the function, a semi-colon is not used in the end.
• The absence of the semi-colon indicates to the compiler that a function is being
defined and not called.
10
Functions Function Prototype
• A function is to be declared in the main( ) function, before it is defined or used.
• However, it is not necessary if the function is returning an int type, but it is
good practice to do so.
• A function prototype is a function declaration that specifies the data types of
the arguments.
• The traditional way to declare a function is by specifying the type of a value to
be returned by the function, and the function name. The Syntax is
type function_name(type parm_nam1, type parm_nam2,… type parm_namN);
example char abc(int x, int y);
11
Functions Function Prototype contd.
• When prototypes are used, C can find and report any illegal type conversions
between the arguments used to call a function and the type definition of its
parameters.
• Also, if there is a difference between the number of arguments used to call a
function and number of parameters in the function, an error will be reported.
• Parameter names are optional. However, they enable the compiler to identify
any type mismatches by name when an error occurs, so it is better to include.
• When a function has no parameters, its prototype uses void inside the
parentheses.
example char noparam( void );
12
Functions Function Prototype contd.
• When a function has no parameters, and any call to that function, which passes
parameters to the function is erroneous.
• When a non-prototyped function is called, all characters are converted to
integers and all floats are converted to doubles.
• However, if a function is prototyped, the types mentioned in the prototype are
maintained and no type promotions occur.
13
Functions Scope of variables
• Variables are name locations in memory that are used to hold a value that may
be modified by the program.
• Variables can be local to a function or a block of code, or global to the entire
program.
• Local Variables are also known as automatic variables, as the keyword auto
can be used to declare them.
• A Local variable is created upon entry into a block and destroyed upon exitfrom
the block.
• As local variables are created and destroyed, the control exits from each entry
and exit from the block, in which they are declared, their contents are lost once
the block is left.
14
Functions Scope of variables contd.
• This implies that they cannot retain their values between calls to functions.
void pgm1(void) • Local variables, which are to be used by the
{
functions, are generally declared
char ch;
ch=‘a’; immediately after the function’s opening
printf(“ch in pgm1( ) is %c”, ch); curly brace and before any other statement.
}
void pgm2(void) • All local variables have to be declared at the
{ start of the block in which they are defined,
char ch; prior to any executable statement.
ch=‘b’;
printf(“ch in pgm2( ) is %c”, ch); Output:
} ch in pgm1( ) is a
ch in pgm2( ) is b
15
Functions Scope of variables contd.
• Global Variables are visible to the program, and can be used by any piece of
code.
• They are declared outside any function of a program and hold their value
through the program execution.
• These variables can be declared either outside the main( ) or declared
anywhere before its first use as long as they are not within a function.
• However, it is best to declare global variables at the beginning of the program,
that is before the main( ) function.
• When a global and local variable have the same name, all references to that
name within the block defining the local variable will be associated with the
local variable and not the global one.
16
Functions Scope of variables contd.
• Storage for global variables is in fixed region of memory set aside for this
purpose by the C compiler.
• Global variables are useful when many functions in a program use the same
data.
• However, unnecessary use of global variables should be avoided mainly
because they take up memory for the entire time that the program is executing.
• Also, using a global variable where a local variable would suffice, makes the
function using it, less general.
--->P.T.O
17
Functions Scope of variables contd.
void addgen(int i, int j ) • Both addgen( ) and addspe( ) return the sum
{ of the variables i and j.
Return (i + j);
} • The addgen( ) function, however, is used to
int i, j
return the sum of any two numbers, while
the addspe( ) function returns only the sum
void addspe(void) of the global variables i and j.
{
return (i + j);
}
18
Functions Calling the Function
• In general, Functions can be communicate with each other by passing
arguments. Arguments can be passed in one of the following two ways:
Call by Value
Call by Reference.
Call by Value:
• In C, by default, all function arguments are passed by value.
• This means that, when arguments are passed to the called function, the values
are passed through temporary variables
• All manipulations are done on these temporary variables only.
• The called function cannot access the actual memory location of the original
variable and therefore cannot change its value.
19
Functions Calling the Function contd.
• The arguments are said to be passed main( )
{
by value.
int a,b,c;
printf(“enter two values”);
• When the value of the variables are scanf(“%d %d”, &a, &b);
passed to the called function and any c= adder(a,b);
printf(“a and b in main( ) are %d %d”, a, b);
alterations on this value has no effect printf(“ addition of a,b in main( )is %d”, c);
on the original value of the passed }
variable. adder( int a, int b)
{
int c;
c=a+b;
a= a*a; b=b+5;
printf(“a and b in adder( ) are %d %d”, a, b);
printf(“ addition of a,b in adder( ) is %d”, c);
return (c);
}
20
Functions Calling the Function
Call by Reference:
• In Call by reference, the function is allowed access to the actual memory
location of the variable passed as an argument .
• Therefore can change the value of the arguments of the calling routine.
• There may be cases, where the values of the arguments of the calling routine
have to be changed.
21
Functions Calling the Function contd.
• The swap( ) function interchanges main( )
values of u and v, but these values {
int a,b;
are not passed back to the main( ).
printf(“enter two values”);
scanf(“%d %d”, &a, &b);
• So if a program is written in such a printf(“a and b in main( ) are %d %d”, a, b);
manner the objective will never be swap(a,b);
achieved. In such a case Call by printf(“After interchanged values are %d”, a, b);
reference is used. }
swap( int u, int v)
{
int temp;
temp=u;
u=v;
v=temp;
return ;
}
22
Functions Recursive Functions
• Recursion is a process of defining something in terms of itself, and is
sometimes called circular definition.
• A function is said to be recursive if a statement in the body of the function calls
itself.
• Each recursive function must specify an exit condition for it to terminate,
otherwise it will go on indefinitely.
• Programs like finding a Factorial, Generating Fibonacci Series uses this
Recursion.
23
Functions Recursive Functions contd.
• The factorial( ) function calculates main()
{
factorial of a given number.
int n; long f;
printf("Enter an integer to find factorial\n");
• This factorial( ) function call itself. scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
• In this function an exit condition is
else
specified in the form of if condition. { f = factorial(n);
printf("%d! = %ld\n", n, f);
}
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
24
Functions Things to remember
The main function returns an integer to the calling process, which is generally
the operating system.
The function main( ) can be declared as void( ), if it does not return a value.
Some compilers issue a warning message if a function is not declared as void
and does not even return a value.
The value returned by main( ) can be checked from DOS through batch files
using internal commands.
Once a function is written, it can be kept in the same file as the main( ) function
or it can be put in a separate file with other functions written by the same user,
or it can be placed into a library.
25
Thank You
26