Functions
Definition
Function is a self-contained block of programs that performs a
coherent task of some kind... Every C program can be thought of as a
collection of these functions.
There are two types of functions
1. Library functions e.g.: printf() , scanf()
2. User defined functions
User defined functions can be broadly divided into two types
1. Functions without arguments
2. Functions with arguments
1. Functions without arguments
Implementation
Three steps
1. Function prototype declaration
Syntax:
return type function name ();
e.g.:
void message();
void italy();
int sum();
2. Function calling
Syntax:
function name();
e.g.:
----
message();
italy();
sum();
3. Function Definition
Syntax:
return type function name ()
{
body of the function;
}
e.g.:
void message()
{
printf(“Welcome to Functions”);
}
e.g.:
#include<stdio.h>
#include<conio.h>
void message();
void main()
{
clrscr();
printf("I am in main function\n");
message();
getch();
}
void message()
{
printf("Welcome to c program");
}
Points to remember
1. C program is a collection of one or more functions.
2. If a program function only one function it must be main().
3. Program execution always begin with main().
4. There is no limit on the number of functions that might be present in
a C program.
E.g.:
#include<stdio.h>
#include<conio.h>
void message();
void usage();
void main()
{
clrscr();
printf("I am in main function\n");
message();
usage();
printf("I am back in main");
getch();
}
void message()
{
printf("I am in message\n");
}
void usage()
{
printf("I am in usage\n");
}
5. Each of the function is called in a sequence specified by the function
calls in the main().
6. After each function has done its thing, its control returns to main().
7. A function gets called when a function name is followed by a
semicolon.
8. A function is defined when function name is followed by a pair of
braces in which one or more statements may present.
9. Any function can be called from any other function. Even main() can
be called from other function. For e.g.
main()
{
message();
}
message()
{
printf(“I am in C \n”);
main();
}
e.g.:
#include<stdio.h>
#include<conio.h>
void italy();
void brazil();
void main()
{
clrscr();
printf("I am in main function\n");
italy();
printf("I am back in main function");
getch();
}
void italy()
{
printf("Welcome to italy\n");
brazil();
printf("I am back in italy\n");
}
void brazil()
{
printf("I am in brazil\n");
}
10. A function can be called any no of times.
11. The order in which the functions are defined in the program and
the order in which they are called need not necessarily be same.
12. A function can call itself. Such a process is called “recursion”.
13. A function can be called from the other function, but a function
cannot be defined in another function.
Advantages of using functions
1. Writing functions avoids rewriting the same code repeatedly.
2. Using functions it is easier to write programs and keep track of what
they are doing.
Functions with arguments
Sometimes we want to communicate between the calling
function and the called function. This is done by using argument.
The mechanism used to convey information to the function is the
argument. The arguments are sometimes also called ‘parameters’.
There are two types of arguments.
1. Actual arguments.
2. Formal arguments.
We must declare the type of the arguments through type declaration
statements before beginning with the statements in the functions.
Syntax:
return type function name(data type arg1, data type arg2,….data
type arg n)
{
statements;
}
Similarly, we can call a function in two ways.
1. call by value
2. Call by reference.
Call by value
Definition
It is a method of passing a photocopy of the values to the calling
functions.
E.g.:
#include<stdio.h>
#include<conio.h>
int add(int x,int y);
void main()
{
int a,b,sum;
clrscr();
printf("Enter any two nos:");
scanf("%d%d",&a,&b);
sum=add(a,b);
printf("Sum=%d\n",sum);
getch();
}
int add(int x,int y)
{ int z;
z=x+y;
return(z);
}
Points to remember
1. In the above program we are passing a photocopy of the value a, b
to the function add(). In add() these values get collected in the
variables x and y.
2. The variables a and b is called as actual arguments. whereas the
variables x and y are called as formal arguments.. Any number of
arguments can be passed to a function being called. However, the
type, order and number of the actual and formal arguments must
always be same.
3. If we want to return some values to the main function then it is done
by using the return statement. The return statement serves two
purposes.
a) On executing the return statement, it immediately transfers the
control back to the calling program.
b) It returns the value present in the parentheses, after return to
the calling program.
4. There is no restriction on the number of return statements that may
be present in a function. In addition, the return statement need not
always be present at the end is called function.
e.g.:
#include<stdio.h>
#include<conio.h>
int code(int d);
void main()
{
int c;
char letter;
clrscr();
printf("Enter a character:");
c=getchar();
letter=code(c);
printf("Letter=%c",letter);
getch();
}
int code(int d)
{
if(d>=65&&d<=97)
return(d+32);
else
return(d);
}
5. Whenever the control returns from a function some value is definitely
returned. If we want the called function should not return any value in
that case, we must mention so by using the keyword void.
6. A function can return one value at a time.
7. If the return statement does not contain any argument then it will
return a garbage value.
8. If the value of the formal argument is changed in the called function,
the corresponding change does not take place in the calling function.
E.g.:
#include<stdio.h>
#include<conio.h>
void num(int b);
void main()
{
int a=20;
clrscr();
num(a);
printf("A=%d",a);
getch();
}
void num(int b)
{
int b=60;
printf("B=%d\n",b);
}
Function declaration and prototypes
Eg:
#include<stdio.h>
#include<conio.h>
float square(float x);
void main()
{
float a,b;
clrscr();
printf("Enter any no:");
scanf("%f",&a);
b=square(a);
printf("Square of %f is %f",a,b);
getch();
}
float square(float x)
{
float y;
y=x*x;
return(y);
}
Scope rule of function
This is decided depending upon the variable, where it is declared.
If the variable is declared inside a function then it is called as
local variables, and the corresponding values of the variables are
available within that function only.
If the variable is declared outside a function, then it is called as
global variables or external variable and the corresponding values of
the variable are available all over the function.
Calling functions with arrays
Array elements can be passed to a function by calling the
function by value or by reference.
In the call by value, we pass values of array elements to the
function, whereas in the call by reference we pass addresses of array
elements to the function.
e.g.:
#include<stdio.h>
#include<conio.h>
void display(int m);
void main()
{
int i;
int marks[]={50,60,70,80,90};
clrscr();
for(i=0;i<5;i++)
display(marks[i]);
getch();
}
void display(int m)
{
printf("%d\n",m);
}
Recursive function
A function can call itself . A function is called ‘recursive’ if a
statement within the body of a function can call the same function.
Sometimes called as circular definition. Recursion is thus the process
of defining something in terms of itself.
e.g.:
#include<stdio.h>
#include<conio.h>
int rec(int x);
void main()
{
int a,fact;
clrscr();
printf("Enter any no:");
scanf("%d",&a);
fact=rec(a);
printf("Factorial value=%d",fact);
getch();
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
Storage classes in C
A variable in storage class tells us
1. Where the variable would be stored.
2. What will be the initial value of the variable if the initial value is not
specifically assigned? (i.e . . . the default initial vale).
3. Scope of the variable. I.e. in which functions the value of the variable
would be available.
4. Life of the variable. i.e. How long would the variable exist.
There are four type of storage class in C.
1. Automatic storage class
2. Register storage class
3. Static storage class
4. External storage class.
1. Automatic storage class
Storage Memory
Default initial value an unpredictable value, which is
often called as garbage value.
Scope Local to the block in which the variable is
defined
Life Till the control remains within the block in
which it is defined.
Eg: 1.
#include<studio.h>
#include<conio.h>
void main ()
{
auto int i;
Clrscr();
printf("i=%d",i);
getch();
}
e.g.: 2
#include<stdio.h>
#include<conio.h>
void main()
{
auto int i=3;
clrscr();
{
{
{
printf("i=%d\n",i);
}
printf("i=%d\n",i);
}
printf("i=%d\n",i);
}
getch();
}
eg: 3
#include<stdio.h>
#include<conio.h>
void main()
{
auto int i=3;
clrscr();
{
auto int i=2;
{
auto int i=1;
printf("i=%d\n",i);
}
printf("i=%d\n",i);
}
printf("i=%d\n",i);
getch();
}
2. Register storage class
Storage CPU registers
Default initial value garbage value
Scope local to the block in which it is
defined.
Life till the control remains within the block in
which it is defined.
e.g.:
---
#include<stdio.h>
#include<conio.h>
void main()
{
register int i;
clrscr();
for(i=1;i<6;i++)
printf("i=%d\n",i);
getch();
}
3. Static storage class
Storage memory
Default initial value zero
Scope local to the block in which it is
defined
Life value of the variable persists between
different function calls.
e.g.:
#include<stdio.h>
#include<conio.h>
void increment();
void main()
{
clrscr();
increment();
increment();
increment();
getch();
}
void increment()
{
static int i=1;
printf("i=%d\n",i);
i=i+1;
}
4. External storage class
Storage memory
Default initial value zero
Scope global
Life as long as the program does not end.
e.g.:
#include<stdio.h>
#include<conio.h>
int i;
void increment();
void decrement();
void main()
{
clrscr();
printf("i=%d\n",i);
increment();
increment();
decrement();
decrement();
getch();
}
void increment()
{
i=i+1;
printf("i=%d\n",i);
}
void decrement()
{
i=i-1;
printf("i=%d\n",i);
}
Advantages of using storage classes
1. Economise the memory space consumed by the variables.
2. Improve the speed of execution of the program.
Rules for using storage class
1. Use static storage class only if we want the value of a variable to
persist between different function calls.
e.g.:
---
Recursive function.
2. Use register storage class for only those variables, which are being
very often used in the program.
e.g.:
---
Loop counters.
3. Use external storage class for only those variables, which are being
used by almost all the functions in the program.
4. If we do not have any of the express needs mentioned above then
use automatic storage class.
Note
------
Default storage class of a variable is automatic.