Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Functions

Introduction to Functions
Defining Functions
Actual And Formal Arguments
Global And Local Arguments
Return Statement
Function Types
Passing Arrays to Functions
Recursive Functions
Functions

A complex program may be decomposed into a small or easily manageable parts or
modules called functions. Functions are very useful to read, write, debug and
modify complex programs. They can also be incorporated in the main program. In
C, the main() itselff is a function that means the main function is invoking
the other functions to perform various tasks.
Advantages of using functions:
* easy to write a correct small functions.
* easy to read, write and debug a function.
* easier to maintain or modify such a function
* small functions tend to be self documenting and highly readable.
* it can be called any number of times in any place with different parameters.
Defining Functions
Defining the function has the following syntax.
functiontype functionname(datatype argument1, datatype argument2......)
{
body of function
--------------
--------------
return (statement);
}
Declaring the type of a function: The function refers to the type of value it
would return to the calling portion of the program. Any of the basic datatypes
such as int, float, char etc. may appear in the function declaration.
example:
int functionname(....);
float functionname(....);
char functionname(....);
Function Name: Normally a function name is made relevant to the function
operation, as it will be easy to keep a track of it, whenever a transfer of
similar functions is used in the main program.
example:
dec
counter();
square();
Formal arguments:
example:
void square(int a,int b)
/*a and b are formal arguments*/
{
----
}
Function body:After declaring the type of a function, function name and formal
arguments, a statement or block of statemnts are enclosed between open and
close braces is a function body.
Actual and formal arguments
The arguments may be classified under two groups actual and formal argument
s.
Actual Arguments: An actual argument is a variable or an expression contained
in a function call that replaces the formal parameter which is a part of the
function declaration.
Sometimes, a function may be called by a portion of a program with some
parameters and these parameters are konwn as the actual arguments.
example:
main()
{
int x,y;
void sum(int x,int y);
------
------
sum(x,y); /*x and y are actual arguments*/
}
Formal Arguments: Formal arguments are the parameters present in a function
definition which may also be called as dummy arguments or the parametric
variables. When the function is invoked, the formal parameters are replaced
by the actual parameters.
example:
main()
{
int x,y;
void sum(int x,int y);
------
------
sum(x,y); /*x and y are actual arguments*/
}
sum(int a,int b) /* a and b are formal or dummy parameters*/
{
-----
}
Local and Global variables
The variables in general may be classified as local or global variables.
Local Variables: The variables which are define inside a function block or a
compound statement is known as local variables.
example:
sum(int a,int b)
{
int x,y;/*local variables*/
----
----
}
Global Variables: Global variables are variables defined outside the main
function block. These variables are refered by the same data type and by the
same name through out the program in both the calling portion of a program and
in the function block.
example:
int a,b=5; /*global variables*/
main()
{
int sum();
a=10;
------
------
sum();
}
sum()
{
int s;
s=a+b;
return s;
}
Return Statement
The keyword return is used to terminate function and return a value to its
caller. The return statement may also be used to exit a function without
returning a value. The return statement may or maynot include an expression.
syntax:
return;
return (exp);
example:
return;
return(345);
return(a+b);
return(++i);
The return statement terminates the execution of the function and pass on the
control back to the calling environment.
/*Example 1 Calculating Power Using Functions*/
Function Types
The functions are mainly divided into two types.
* Library Functions.
* User defined Functions.
Libraries of Functions: The following are the standard libraries of functions in
C.
<assert.h>
<ctype.h>
<errno.h>
<float.h>
<limits.h>
<locale.h>
<math.h>
<setjmp.h>
<signal.h>
<stdarg.h>
<stddef.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
User defined Functions:
The user defined functions are classified in 3 ways based on the formal
arguments passed and the usage of the return statement.
functions with no arguments and no return types
functions with arguments and no returntypes
functions with arguments and returntypes
Function with no arguments and no return Types
It is the simplest way of writing a user defined function in C. There is no

data communication between the calling portion of a program and a called
function block. The function is invoked by a calling environment by not
passing any formal argument and the function also does not return back any
value to the caller.
example:
main()
{
clrscr();
add(); /*calling function*/
}
add() /*called function*/
{
float x,y;
printf("/nenter 2 floating point nos:");
scanf("%f%f",&x,&y);
printf("/nsum of two floating point nos=%f",x+y);
}
Function with arguments and no return Types
The second type of user defined function passes some formal arguments to a
function but the function does not return back any value to the caller. It is
a one-way data communication between a calling portion of a program and the
function block.
example:
/* Exmaple 2 Function with arguments and no return Types */
Function with arguments and return Types
The third type of user defined function passes some formal arguments to a
function from a calling portion of the program. And the computed value, if
any is transfered back to the caller. Data are communicated between both the
calling portion of a program and the function block.
example:
/*example 3 Function with arguments and return Types*/
/*example 4 What Happens When Formal Arguments Are More Than Actual Arguments*/
Passing Arrays to Functions
The entire array can be passed on to a function in C. An array name can be
used as an argument for the function declaration. No subscripts are required
to invoke a function using arrays.
example:
main()
{
int sumarray(int a[],int n);
int a[10],sum;
--------------
--------------
sum(a,n); /*passing array to Function*/
}

Recursive Functions
A function which calls itself directly or indirectly again and again is kno
wn as the recursive function. Recursive functions are very useful while
constructing the data structures like linked lists, double linked lists and
trees.
There is a dintinct difference between normal and recursive functions. A
normal function will be invoked by the main function whenever the function name
is used. whereas the recursive function will be invoked by itself directly or
indirectly as long as the given condition is satisfied.
example:
recursive function for factorial is n*fact(n-1)
It works in the following way. Let us assume n=5
5*fact(5-1)->5*fact(4)
4*fact(4-1)->4*fact(3)
3*fact(3-1)->3*fact(2)
2*fact(2-1)->2*fact(1)
1*fact(1-1)->1*fact(0)
syntax:
main()
{
function(); /*calling function*/
--------
--------
}
function()
{
--------
--------
function();/*function call recursively*/
}
/*example 5 Finding Factorial Using Recurssion*/

You might also like