Unit 2 Functions
Unit 2 Functions
COMPUTER
PROGRAMMING
TYPES OF FUNCTION
There are two types of function in C programming:
1. Standard library functions
2. User-defined functions
2. Calling a function
Whenever Compiler will call the Function , the Control of the
program is transferred to the user-defined function by calling it.
Syntax:
functionName(argument1, argument2, ...);
Example :
addition(a,b);
3. Function definition
Function definition contains the block of code to perform a
specific task. In our example, adding two numbers and returning it.
When a function is called, the control of the program is transferred
to the function definition. And, the compiler starts executing the
codes inside the body of a function.
ADVANTAGES OF FUNCTIONS
Avoid repetition of codes And Increases program readability.
Divide a complex problem into simpler ones.
Reduces chances of error.
Modifying a program becomes easier by using function.
There is no limit in calling C functions to make use of same
functionality wherever required. We can call functions any number
of times in a program and from any place in a program.
Program development become easy
A function can call other functions & also itself
TYPES OF FUNCTION ARGUMENTS IN C
Basically, there are two types of arguments:
1. Actual arguments
2. Formal arguments
1. Actual Arguments
The values that are passed to the called function from the main
function are known as Actual arguments.
2. Formal Arguments
The variables declared in the function prototype or definition are
known as Formal arguments or Dummy arguments.
These arguments are used to just hold the value that is sent by
calling function.
Formal arguments are like other local variables of the function
which are created when function call starts and destroyed at the end
of function.
TYPES OF FUNCTION CALLS IN C
we can call a function in two different ways, based on how we specify
the arguments, and these two ways are:
1. Call by Value
2. Call by Reference
CALL BY VALUE
In call by value method, the value of the actual parameters is
copied into the formal parameters. In other words, we can say
that the value of the variable is used in the function call in the call
by value method.
In call by value method, we cannot modify the value of the
actual parameter by the formal parameter.
In call by value, different memory is allocated for actual and
formal parameters since the value of the actual parameter is
copied into the formal parameter.
Example:
#include<stdio.h>
#include<conio.h>
int fun(int a,int b);
void main()
{
clrscr();
int a=10;
int b=20;
fun(a,b);
printf("Value of A is : %d",a);
printf("\nValue of B is : %d",b);
getch();
}
int fun(int a,int b)
{
a=20;
b=10;
}
Output:
int main()
{
MYFUNCTION();
}
How it works?
ADVANTAGES OF RECURSION
Reduce unnecessary calling of function.
Through Recursion we can Solve problems in easy way while its
iterative solution is very big and complex.
Recursion function required less coding.
Recursion adds clarity and reduces the time needed to write and
debug code.
DISADVANTAGES OF RECURSION
Recursive solution is always logical and it is very difficult to trace.
In recursive we must have an if statement to force the function for
termination of calling, otherwise the function goes in infinite state.
Recursion uses more processor time.
If proper coding is not done then also Recursive function may lead
to infinite loop.
STORAGE CLASSES
A storage class represents the visibility and a location of a
variable. It tells from what part of code we can access a variable.
A storage class in C is used to describe the following things:
The variable scope.
The location where the variable will be stored.
The initialized value of a variable.
A lifetime of a variable.
Who can access a variable?
We have four different storage classes in a C program;
1. auto
2. register
3. static
4. extern
1. The auto Storage Class:
The auto storage class is the default storage class for all local
variables.
Example: {
int a;
auto int b;
}
The example above defines two variables with in the same storage
class. 'auto' can only be used within functions, i.e., local variables.
2. The register Storage Class:
You can use the register storage class when you want to store local
variables within functions or blocks in CPU registers instead of
RAM to have quick access to these variables.
For example, "counters" are a good candidate to be stored in the
register.
Example: register int age;
The keyword register is used to declare a register storage class. The
variables declared using register storage class has lifespan
throughout the program.
It is similar to the auto storage class. The only difference is that the
variables declared using register storage class are stored inside
CPU registers instead of a memory. Register has faster access than
that of the main memory.
3. The static Storage Class:
The static variables are used within function/ file as local static
variables. They can also be used as a global variable.
Static local variable is a local variable that retains and stores its
value between function calls or block in which it is defined.
Example: static int count = 10;
Static variable has a default initial value zero and is initialized only
once in its lifetime.
4. The extern Storage Class:
Extern stands for external storage class. Extern storage class is
used when we have global functions or variables which are shared
between two or more files.
Example: extern i;
Keyword extern is used to declaring a global variable or function
in another file to provide the reference of variable or function
which have been already defined in the original file.
SUMMERY
#include<stdio.h>
#include<conio.h>
static int a = 5;
void main()
{
static int b=10;
register i = 15;
auto d = 20;
clrscr();
printf("Value of static global variable a is : %d",a);
printf("\nValue of static local variable b is : %d",b);
printf("\nValue of register variable i is : %d",i);
printf("\nValue of auto global variable d is : %d",d);
getch();
}
Output:
Value of static global variable a is :5
Value of static local variable b is :10
Value of register variable i is :15
Value of auto global variable d is : 20