Functions
In programming, a function is a segment that groups code to perform a specific task.
A C program has at least one function main( )
. Without main( )
function, there is technically no C program.
Types of C functions
There are two types of functions in C programming:
- Library function
- User defined function
Library function
Library functions are the in-built function in C programming system. Function prototype and data definitions of these functions are written in their respective header file. For example: If you want to use printf()
function, the header file
should be included.
For example : main( ) - The execution of every C program starts from this main()
function.
printf ( ) - prinf()
is used for displaying output in C.
scanf ( ) - scanf()
is used for taking input in C.
There is at least one function in any C program, i.e., the main()
function (which is also a library function). This program is called at program starts.
There are many library functions available in C programming to help the programmer to write a good efficient program.
Suppose, you want to find the square root of a number. You can write your own piece of code to find square root but, this process is time consuming and the code you have written may not be the most efficient process to find square root. But, in C programming you can find the square root by just using sqrt()
function which is defined under header file "math.h".
1. write a program to find square root of a number using library function.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
# include < math.h >
void main ( )
{
int num, root ;
clrscr ( ) ;
Printf ( ” enter a number to find square root \ n ” ) ;
Scanf ( ” % d ”, &num ) ;
root = sqrt ( num ) ;
Printf ( ” square root of % d = % d ”, num, root ) ;
}
isalpha ( )
In C programming, isalpha( ) function checks whether a character is an alphabet(a to z and A-Z) or not. If a character passed to isalpha() is an alphabet, it returns non-zero integer if not it returns 0.
Defined in Header file: < ctype.h >
2. C Program to Check whether a Character Entered by User is Alphabet or not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
# include < ctype.h >
void main ( )
{
char c ;
clrscr ( ) ;
Printf ( ” enter character \ n ” ) ;
Scanf ( ” % c ”, &c ) ;
if ( isalpha ( c ) == 0 )
printf ( " given character is not an alphabet " ) ;
else
printf ( " given character is an alphabet " ) ;
}
isdigit()
In C programming, library function isdigit() checks whether a character is numeric character(0-9) or not. If a character passed to isdigit() is a digit, it returns non-zero integer and if not it returns 0.
Defined in Header file:< ctype.h >
3. Write a Program to Check whether a Character Entered by User is Numeric Character or Not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
# include < ctype.h >
void main ( )
{
char c ;
clrscr ( ) ;
Printf ( ” enter character \ n ” ) ;
Scanf ( ” % c ”, &c ) ;
if ( isdigit ( c ) == 0 )
printf ( " given character is not a digit " ) ;
else
printf ( " given character is a digit " ) ;
}
isalnum()
In C programming, isalnum( ) function checks whether a character is an alphanumeric chraracter(a-z or A-Z or 0-9) or not. If a character passed to isalnum( ) is alphanumeric(either alphabet or number), it returns non-zero integer if not it returns 0.
Defined in Header file: < ctype.h >
4. Write a Program to Check whether a Character Entered by User is Alphanumeric or Not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
# include < ctype.h >
void main ( )
{
char c ;
clrscr ( ) ;
Printf ( ” enter character \ n ” ) ;
Scanf ( ” % c ”, &c ) ;
if ( isalnum ( c ) == 0 )
printf ( " given character is not an alpha numeric character " ) ;
else
printf ( " given character is an alpha numeric character " ) ;
}
User defined function
C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions. Suppose, a programmer wants to find factorial of a number and check whether it is prime or not in same program. Then, he/she can create two separate user-defined functions in that program: one for finding factorial and other for checking whether it is prime or not.
Remember, the function name is an identifier and should be unique.
Advantages of user defined functions
- User defined functions helps to decompose the large program into small segments which makes programmer easy to understand, maintain and debug.
- If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.
- Programmer working on large project can divide the workload by making different functions.
1.Write a C program to add two integers. Make a function add
to add integers and display sum in main()
function.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int add ( int a, int b ) ; //function prototype(declaration)
int main ( )
{
int num1, num2, sum ;
clrscr ( ) ;
Printf ( ” enter two numbers to add\ n ” ) ;
Scanf ( ” % d % d ”, &num1, &num2 ) ;
sum = add ( num1, num2 ) ; //function call
Printf ( ” sum = % d ”, sum ) ;
}
int add ( int a, int b ) //function declarator
{
/* Start of function definition. */
int add ;
add = a + b ;
return add ; //return statement of function
/* End of function definition. */
}
Function prototype(declaration):
Every function in C programming should be declared before they are used. These type of declaration are also called function prototype. Function prototype gives compiler information about function name, type of arguments to be passed and return type.
In the above example,int add(int a, int b);
is a function prototype which provides following information to the compiler:
- name of the function is
add()
- return type of the function is
int
. - two arguments of type
int
are passed to function.
Function prototype are not needed if user-definition function is written before main()
function.
Function call
Control of the program cannot be transferred to user-defined function unless it is called invoked.
In the above example, function call is made using statement add(num1,num2);
from main()
. This make the control of program jump from that statement to function definition and executes the codes inside that function.
Function definition
Function definition contains programming codes to perform specific task.
1. Function declarator
Function declarator is the first line of function definition. When a function is called, control of the program is transferred to function declarator.
2. Function body
Function declarator is followed by body of function inside braces.
Return Statement
Return statement is used for returning a value from function definition to calling function.
Recursion
A function that calls itself is known as recursive function and this technique is known as recursion in C programming.
( or )
In C programming language, when a function calls itself over and over again, that function is known as recursive function. The process of function calling itself repeatedly is known as recursion.
1.Write a C program to find sum of first n natural numbers using recursion.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int sum ( int n ) ;
int main ( )
{
int num, add ;
clrscr ( ) ;
Printf ( ” enter number \ n ” ) ;
Scanf ( ” % d ”, &num ) ;
add = sum ( num) ;
Printf ( ” sum = % d ”, add ) ;
}
int sum ( int n )
{
if ( n == 0 )
return n ;
else
return n + sum ( n - 1 ) ;
}
In, this simple C program, sum()
function is invoked from the same function. If n is not equal to 0 then, the function calls itself passing argument 1 less than the previous argument it was called with. Suppose, n is 5 initially. Then, during next function calls, 4 is passed to function and the value of argument decreases by 1 in each recursive call. When, n becomes equal to 0, the value of n is returned which is the sum numbers from 5 to 1.
For better visualization of recursion in this example:
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In this example when, n is equal to 0, there is no recursive call and recursion ends.
2. Write a program to Calculate Factorial Using Recursion
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int factorial ( int n ) ;
int main ( )
{
int n ;
clrscr ( ) ;
Printf ( ” enter number \ n ” ) ;
Scanf ( ” % d ”, &n ) ;
Printf ( ” factorial = % d ”, factorial ( n ) ) ;
return 0 ;
}
int factorial ( int n )
{
if ( n = = 0 )
return = 1 ;
else
return n * factorial ( n - 1 ) ;
}
3. Write a program to Calculate HCF Using Recursion
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
int hcf ( int n1 , int n2 ) ;
int main ( )
{
int n1 , n2 ;
clrscr ( ) ;
Printf ( ” enter n1 , n2 \ n ” ) ;
Scanf ( ” % d % d ”, & n1 , & n2 ) ;
Printf ( ” HCF = % d ”, hcf ( n1 , n2 ) ) ;
return 0 ;
}
int hcf ( int n1 , int n2 )
{
if ( n2 ! = 0 )
return hcf ( n2 , n1 % n2 ) ;
else
return n1 ;
}
4. Write a program to reverse a sentence Using Recursion
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void reverse ( ) ;
int main ( )
{
clrscr ( ) ;
Printf ( ” enter sentence\ n ” ) ;
reverse ( ) ;
return 0 ;
}
void reverse ( )
{
char c;
Scanf ( ” % c ”, & c ) ;
if ( c ! = " \ n " )
{
reverse ( ) ;
Printf ( ” % c ” , c ) ;
}
}
Storage Class
Every variable in C programming has two properties: type and storage class. Type refers to the data type of variable whether it is character or integer or floating-point value etc. And storage class determines how long it stays in existence.
There are 4 types of storage class:
- automatic
- external
- static
- register
1. Automatic storage class
Keyword for automatic variable : auto
Variables declared inside the function body are automatic by default. These variable are also known as local variables as they are local to the function and doesn't have meaning outside that function
Since, variable inside a function is automatic by default, keyword auto are rarely used.
2. External storage class
External variable can be accessed by any function. They are also known as global variables. Variables declared outside every function are external variables.
In case of large program, containing more than one file, if the global variable is declared in file 1 and that variable is used in file 2 then, compiler will show error. To solve this problem, keyword extern
is used in file 2 to indicate that, the variable specified is global variable and declared in another file.
Example to demonstrate working of external variable
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void check ( ) ;
int a = 5 ; /* a is global variable because it is outside every function */
int main()
{
a = a + 4 ;
Check ( ) ;
return 0 ;
}
void Check ( )
{
++ a ; /* Variable a is not declared in this function but, works in any function as they are global variable */
printf ( " a = % d \ n " , a ) ;
}
3. Register Storage Class
Keyword to declare register variable : register
Example of register variable
register int a;
Register variables are similar to automatic variable and exists inside that particular function only.
If the compiler encounters register variable, it tries to store variable in microprocessor's register rather than memory. Value stored in register are much faster than that of memory.
In case of larger program, variables that are used in loops and function parameters are declared register variables.
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.
4. Static Storage Class
The value of static variable persists until the end of the program. A variable can be declared static using keyword : static
.
For example : static int i ;
Example to demonstrate the static variable
Example to demonstrate working of external variable
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void check ( ) ;
int main()
{
Clrscr ( ) ;
Check ( )
;
Check ( ) ;
Check ( ) ;
}
void Check ( )
{
static int c = o ;
printf ( " % d \ t " , c ) ;
c = c + 5 ;
}