Function Of C
Function Of C
functions. All that a programmer has to do is use them straight away. For
example, if you have to find length of a string , use strlen() without having to
write the required code.
The code for all standard functions is available in library files, like cs.lib, and
graphics.lib. These library files are supplied by the vendor of compiler. Where
these libraries are stored in the system depends on the compiler. For example, if
you are using Turbo C, you find libraries in LIB directory under directory where
Turbo C is installed.
Declarations about standard functions are available in header files such as stdio.h,
and string.h. Turbo C provides around 400 functions covering various areas like
Screen IO, graphics, disk IO etc.
User-defined functions
User-defined function is a function that is defined by user. That means, the code
for the function is written by user (programmer). User-defined functions are
similar to standard functions in the way you call. The only difference is instead of
C language providing the function, programmer creates the function.
If a program is divided into functional parts than each part may be independently coded
and later combined into single unit.
Advantages of Functions:
The length of the source program can be reduced by using functions at
appropriate places.
It becomes easier to locate a faculty functions.
Many other programs may use a function.
}
Single large main() function
main()
{
takeinput();
process();
displayoutput();
}
takeinput()
{
}
process ()
{
}
displayoutput()
{
}
Function Declaration:
A function may contain declaration and definition. Function declaration specifies
function name, return type, and type of parameters. This is normally given at the
beginning of the program.
Passing Parameters:
A parameter or argument is a value passed to function so that function can use that value
while performing task. A function may take none, one or more parameters depending
upon the need.
Example: line(); /*function that pass no parameters*/
line(20); /*function that pass one parameter*/
getaverage(10,20); /*function that pass more than one parameter*/
Actual Parameter: It is the value that is passed to function while calling the function. For
example; 10 and 20 values that we passed to line() and getaverage() function in the
above are actual parameters.
Formal Parameter: Variable that is used to receive actual parameter is called formal
parameter.
Example: getaverage(int a, int b); Here a and b are formal parameters.
Returning Value:
Normally after performing the task functions return a value. The return value may be of
Prepared by : Prof. Jahnavi Shukla Page 115
any type. But a function can return only one value. To return a value from the function,
we have to first specify what type of value the function returns and then we have to use
return statement in the code of the function to return the value.
Category of Functions:
A function depending on whether arguments are present or not and whether a value is
returned or not may belong to one of the following categories:
1. Functions with no arguments and no return value.
2. Functions with arguments and no return value.
3. Functions with arguments and return value.
4. Functions with no arguments and return value.
Functions with no arguments and no return value
When a function has no argument, it does not receive any data from calling function.
When it does not return a value, the calling function does not receive any data from
called function. In effect, there is no data transfer between calling function & called
function.
Program 1:
#include<stdio.h>
{
printf("Addition of 2 numbers:\n");
sum();
}
void
sum()
{ int a,b;
printf("Enter 2 numbers: ");
scanf("%d %d",&a,&b);
printf("\nSum of %d and %d is %d",a,b,a+b);
}
Prepared by : Prof. Jahnavi Shukla Page 117
Functions with no arguments and return value
When a function has no argument, it does not receive any data from calling function.
When it does return a value, the calling function receives data from called function. In
effect, there is data transfer between called function & calling function.
Program:
#include<stdio.h>
float average();
main()
{
float avg;
printf("Average of 3 numbers:\n");
avg = average();
printf("Average: %f",avg);
}
float average()
{
int a,b,c,s;
printf("Enter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
s=a+b+c;
return s/3.0;
}
Functions with arguments and no return value
When a function has argument(s), it does receive data from calling function. When it
does not return a value, the calling function receives does not data from called function.
In effect, there is data transfer between calling function & called function.
Program 1:
#include<stdio.h>
void dline ();
main()
{
dline(15);
printf("\nFunctions...");
dline(15);
getch();
}
void dline(int n)
{
int l;
for(l=0;l<n;l++)
putch('-');
}
Program 2:
#include<stdio.h>
void interest(float,int,float);
main()
Prepared by : Prof. Jahnavi Shukla Page 118
{
int t;
float p,r;
printf("Enter principal amount: ");
scanf("%f",&p);
printf("Enter time period: ");
scanf("%d",&t);
printf("Enter rate: ");
scanf("%d",&r);
interest(p,t,r); // function call….
}
void interest(float pr,int tp,float rate) // function defination
{
float si;
Blocks:
C is a block structured language. Blocks are delimited by { and }. Every block can have its
own local variables. Blocks can be defined wherever a C statement could be used. No
semi-colon is required after the closing brace of a block.
Scope
The area of the program from where a variable can be accessed is called as scope of the
variable. The scope of a variable determines over what parts of the program a variable is
actually available for use (active). Scope of the variable depends on where the variable is
declared. The following code illustrate the scope of a variable:
int g; /* scope of g is from this point to end of program */
main()
{
int x; /* scope of x is throughout main function */
. . . .
}
void sum()
{
int y; /* scope of y is throughout function sum */
. . . .
}
You cannot change the scope of a variable, it is completely dependent on the place of
declaration in the program.
Visibility:
Normally visibility and scope of variables are same. In other words, a variable is visible
throughout its scope.
Visibility means the region of the program in which a variable is visible. A variable can
never be visible outside the scope. But it may be invisible inside the scope.
int g; /* scope of g is from this point to end of program */
main()
{
int x; /* scope of x is throughout main function */
. . . .
}
Prepared by : Prof. Jahnavi Shukla
void sum()
{
int y; /* scope of y is throughout function sum */
int g;
g=20; /* local variable g is used not global variable g */
}
In the above code, variable g has global scope. But it becomes invisible throughout the
function sum as a variable with the same name is declared in that function.
When a global variable is re-declared in a function then the local variable takes
precedence i.e., g in function sum will reference g in function but not g declared as global
variable. So, in this case though g has scope throughout program, it is not visible
throughout function sum.
For global variable to get its visibility in a function, when a local variable with same
variable name is declared and use we use scope resolution operator(::). This helps in
accessing global variable even though another variable with same name is existing.
int g; /* scope of g is from this point to end of program */
main()
{
int x; /* scope of x is throughout main function */
. . . .
}
void sum()
{
int y; /* scope of y is throughout function sum */
int g;
g=20; /* local variable g is used not global variable g */
::g=g+10; /*global variable g is used now with the operator*/
/*'::' called scope resolution operator*/
}
Extent/Lifetime
Also called as Longevity. This refers to the period of time during which a variable resides
in the memory and retains a given value during the execution of a program. Longevity
has a direct effect on the utility of a given variable.
A variable declared inside a block has local extent. Because it exists in the memory as
long as the block is in execution. It is created when block is invoked and removed when
the execution of the block is completed.
On the other hand, variable declared outside all functions has static extent as they remain
in memory throughout the execution of program.
Since, there are limited number of register in processor and if it couldn't store the variable
in register, it will automatically store it in memory.
Example:
main()
{
register int i;
for(i=0;i<10000;i++)
. . . .
}
When you use static storage class while declaring local variable, instead of creating the
variable and initializing it whenever control enters the block, the variable is created an d
initialized only for once - when variable's declaration is encountered for first time. So a
variable with static extent will remain in the memory across function calls.
Storage Location: Main Memory
Default Value: Zero
Scope: Local to the block in which variable is declared
Lifetime: till the value of the variable persists between different function calls.
Example: Program to illustrate the properties of a static variable.
main() {
int i;
for (i=0; i<3; i++)
incre();
}
incre() {
int avar=1;
static int svar=1;
avar++; svar++;
printf("\n Automatic variable value : %d",avar);
printf("\t Static variable value : %d",svar);
}
Output:
Automatic variable value: 2 Static variable value: 2
Automatic variable value: 2 Static variable value: 3
Automatic variable value: 2 Static variable value: 4
/* File2.c */
#include"File1.c"
int count; /* creates variable count with static extent */
main() {
. . .
}
In this case program File1.c has to access the variable defined in program File2.c.
Storage Location: Main Memory
Default Value: Zero
Scope: Global/File Scope
Lifetime: as long as the program execution doesn't come to an end.
Example: Program to illustrate extern storage class
write.c
void write_extern();
extern int count;
void write_extern()
{
count = count + 2;
printf("Count is %d\n",count);
Output:
}
Count is 7
mmain.c Count is 9
#include"write.c"
int count=5;
main() {
write_extern();
write_extern();
}
Also, when you define the parameter to read in the data in the function prototype, you
declare it as though you were reading in a single-item variable instead of an array. When
an array is passed as parameter, only the name of the array is given at the time of calling
function the formal parameter is to be declared as an array.
If Function A needs to use the result of Function B, function A has to use the name of
function B. This means that Function A has to “call” Function B:
When calling one function from another function, provide neither the return value nor
the body, simply type the name of the function and its list of arguments, if any.
Example: Program to call function inside a function.
main()
{
processinput();
getch();
}
processinput()
{
int a,b,sum;
printf("Enter 2 numbers: ");
scanf("%d %d",&a,&b);
sum = process(a,b);
displayresult(a,b,sum);
}
process(int a,int b)
{
return a+b;
}
displayresult(int x,int y,int s)
Recursion
long fact(int n)
{
int m;
if (n == 1)
return n;
else
return n * fact(n-1);
}
{
int x, y;
if(n==0)
return 0;
else if(n==1)
return 1;
else
{
Prepared by : Prof. Jahnavi Shukla Page 137
x= fibno( n-1);
y= fibno( n-2);
return (x+y);
}
}