Unit3 C Program
Unit3 C Program
Main() function:
Without main() function we can write any program in C. Every program must have a main function
to indicate where the program has to begin its execution.
Function:
Function is a set of executable statements written outside of the main function. It is used to perform
a well-defined task.
Advantages of function:
The length of the source program can be reduced by function because repeated statements will be
written in the function.
It is easy to locate and isolate a faulty function for further investigations.
A function may be used by many other programs.
Function prototype:
Function declaration is also called function prototype. It tells three things about the function to the
compiler. They are
1. What is the name of the function?
2. What is the return type of the function?
3. No. of arguments (parameter) and their types?
Syntax:
return_type function_name( parameter list);
Ex:
void show(void);
Function Name
Function name must be a valid variable. What are the procedures applicable for a variable is also
applicable to a function.
Return Type
In return type we have to specify the data type. It specifies which type of data should be returned by
the function after its execution.
Arguments
Arguments are data items that are needed by the function to complete its task.
Note: void is a special keyword specified in the return type, it tells the compiler that function won’t return
any value after its execution.
MS Page 1
UNIT – III Programming in C
If we specify the keyword void in the argument, it tells the compiler that function doesn’t want any
data item externally to complete its task.
Definition of functions:
A function definition or function implementation has the following elements. Such as
1. Function name
2. Return type
3. List of parameters
4. Local variable declaration
5. Function statements
6. Return statement
Ex:
void show(void)
{
int a;
printf(“Enter your a value :”);
scanf(“%d”,&a);
printf(“Your value is : %d”,a);
}
In the above example name of the function is show, return type of the function is void, it does not
require any parameters so argument type is void, a is a local variable inside the function.
Example:
#include<stdio.h>
#include<conio.h>
MS Page 2
UNIT – III Programming in C
getch();
}
void print(int p,int q) /* called function */
{
printf("Your values are :\n");
printf(" A : %d\n",p);
printf(" B : %d\n",q);
}
Output:
Enter your a and b :
50
80
Your values are :
A : 50
B : 80
Calling function:
Function calling statement is also called calling function. In the above program print(a,b) is the
calling function.
Called function:
Definition of the corresponding function calling statement is called, called function. In the above
program void print(int p,int q) is the called function.
Actual arguments:
Arguments that are specified in the function calling statement are called actual arguments. In the
above program arguments a and b are called actual arguments, because they are specified in the function
calling statement.
Formal arguments:
Arguments that are in the called function are called formal arguments. In the above example p and q
are formal arguments.
Category of functions:
A function may be one of the following categories.
Function with no arguments and no return value.
Function with arguments and no return value.
MS Page 3
UNIT – III Programming in C
MS Page 4
UNIT – III Programming in C
Example:
#include<stdio.h>
#include<conio.h>
MS Page 5
UNIT – III Programming in C
Example:
#include<stdio.h>
#include<conio.h>
int fact(int x)
{
int i,p=1;
for(i=1;i<=x;i++)
p=p*i;
return p;
}
Output
Enter your n value : 6
Factorial value for 6 is : 720
Function with no arguments but return value:
When a function has no arguments, it does not receive any data from calling function. But in this
case called function will return a value to the calling function after its execution.
MS Page 6
UNIT – III Programming in C
Example:
#include<stdio.h>
#include<conio.h>
int add(void);
void main()
{
int x;
clrscr();
x=add();
printf("Result is : %d",x);
getch();
}
int add(void)
{
int a,b;
printf("Enter your a and b values :\n");
scanf("%d%d",&a,&b);
return(a+b);
}
Output:
Enter your a and b values :
10
30
Result is : 40
Recursion:
A function calls itself is called recursive function. A recursive procedure must have two properties.
1. There must be certain criterion called base criteria for the procedure does not call itself.
2. Each time function refers to itself, the argument of the function must be closer to the base criterion.
MS Page 7
UNIT – III Programming in C
Example:
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int n;
clrscr();
printf("Enter your n value :");
scanf("%d",&n);
printf("\nThe factorial value is : %d",factorial(n));
getch();
}
int factorial(int n)
{
if(n<=1)
return 1;
else
return(n*factorial(n-1)); /* recursive function call */
}
Output
Enter your n value : 5
The factorial value is : 120
Storage classes:
Storage class tells four things about a variable to the compiler.
What is the scope of the variable?
What is the lifetime of the variable?
What is the default value of the variable?
Where will it take memory for its storage?
Scope: It refers what region of program the variable is available.
Lifetime: It refers how long the value of the variable is available for usage.
MS Page 8
UNIT – III Programming in C
Default value: When a variable is declared what value will be in by default. It may be zero or garbage.
Storage: Where it will allocate memory space to store value. It may CPU registers or memory.
The following storage classes are available in C.
1. Automatic variables.
2. External variables.
3. Static variables.
4. Register variables.
Automatic Variables:
It is the default storage class for a variable. We can use the keyword auto to declare automatic
variables. Automatic variables are declared and accessed inside a function. They are created when the
function is called and destroyed automatically when the function is exited.
Scope: It is a local scope variable. We can access within a function
Life time: Its value is available until the compiler exits from the function.
Default: Its default value is garbage (unpredictable).
Storage: It will take memory spaces in computer memory.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=100;
clrscr();
{
int a=250;
{
int a=300;
printf("\na = %d",a);
}
printf("\na = %d",a);
}
printf("\na = %d",a);
MS Page 9
UNIT – III Programming in C
getch();
}
Output:
a = 300
a = 250
a = 100
External Variables:
External variables are alive and active throughout the entire program. It is also called global
variables. External variables are declared outside of all the functions. We can declare external variable with
the keyword extern.
Scope: It is a global scope variable. We can access anywhere in our program.
Life time: Its value is available until the compiler terminates the program execution.
Default: Its default value is Zero (0).
Storage: It will take memory spaces in computer memory.
Example:
#include<stdio.h>
#include<conio.h>
MS Page 10
UNIT – III Programming in C
void print(void)
{
printf("\n print function ++x = %d",++x);
}
Output:
print function ++x = 11
main function ++x = 12
print function ++x = 13
main function ++x =14
print function ++x = 15
Static Variables:
Static variable is just like an automatic variable but its value exists between different function call.
We can declare static variable with the keyword static.
Scope: It is a local scope variable. We can access within the function.
Life time: Its value is available between function calls.
Default: Its default value is Zero (0).
Storage: It will take memory spaces in computer memory.
Example:
#include<stdio.h>
#include<conio.h>
void print(void)
{
static int a=50; /* static variable */
printf(" A = %d\n",a);
a++;
}
void main()
{
clrscr();
print();
MS Page 11
UNIT – III Programming in C
print();
print();
getch();
}
Output:
A = 50
A = 51
A = 52
Register Variables:
We can declare register type variable by using the keyword register. It will take memory spaces for
its storage in CPU register. So it can be accessed faster than any other variables during the execution.
Scope: It is a local scope variable. We can access within the function.
Life time: Its value is available until the compiler exits from the function.
Default: Its default value is garbage (unpredictable).
Storage: It will take memory spaces in CPU registers.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
register int a=50; /* register variable */
clrscr();
printf("The register variable a = %d",a);
getch();
}
Output:
The register variable a = 50
MS Page 12