C MODULE 4
C MODULE 4
Function in C
Type of Function in C
1. Library function or pre-define function.
2. User defined function.
Library function:
Library functions are those which are predefined in C compiler.
Library functions are built-in functions that are grouped together and
placed in a common location called library.
All C standard library functions are declared by using many header files..
Advantages of Using Library Functions:
1. Saves time: Library functions provide prewritten code that can be used to
perform common programming tasks, saving programmers time and effort.
2. Increases productivity: With library functions, programmers can quickly
develop complex programs without worrying about the low-level details.
3. Improves code readability: Library functions provide a standardized way
of performing common tasks, making the code more readable and easier to
maintain.
4. Reduces bugs: The use of library functions reduces the chances of
introducing bugs into the program since the code has already been tested
and debugged.
In c, we can divide a large program into the basic building blocks known as
function. The function contains the set of programming statements enclosed by
{}. A function can be called multiple times to provide reusability and modularity
to the C program. In other words, we can say that the collection of functions
creates a program. The function is also known as procedureor subroutinein other
programming languages.
Advantage of functions in C
{
statements;
return x;
}
Example for Function with argument and with return value
#include<stdio.h>
int sum(int, int);
void main()
{ int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
getch();
}
int sum(int a, int b)
{
return a+b; }
2. Function with arguments but no return value
When a function has arguments, it receives any data from the calling function but
it returns no values. These are void functions with no return values.
Syntax:
Function declaration : void function ( int );
Function call : function( x );
Function definition:
void function( int x )
{
statements;
}
Example:
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
getch();}
4. Function with no arguments but returns a value
There could be occasions where we may need to design functions that may not
take any arguments but returns a value to the calling function. An example of this
is getchar function which has no parameters but it returns an integer and integer-
type data that represents a character.
Syntax:
Function declaration : int function();
Function call : function();
Function definition :
int function()
{
statements;
return x;
}
Example for Function without argument and with return value
#include<stdio.h>
int sum();
void main()
{
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b; getch();
}
Recursion in C
Recursion is the process of a function calling itself repeatedly till the given
condition is satisfied.
A function that calls itself directly or indirectly is called a recursive
function and such kind of function calls are called recursive calls.
In C, recursion is used to solve complex problems by breaking them down
into simpler sub-problems.
We can solve large numbers of problems using recursion in C.
For example, factorial of a number, sum of natural numbers, etc.
Recursive Functions in C
A recursive function performs the tasks by dividing it into the subtasks. There is
a termination condition defined in the function which is satisfied by some specific
subtask. After this, the recursion stops and the final result is returned from the
function.
Basic Structure of Recursive Functions
type function_name (args) {
// function statements
// base condition
// recursion case (recursive call)
}
example:
#include <stdio.h>
int sum(int);
void main()
{
int number;
printf("Enter a positive integer :");
scanf("%d", &number);
printf("The sum of first %d numbers is %d.", number, sum(number));
getch();
}
int sum(int n)
{
if (n == 0)
return 0;
else
return n + sum(n - 1);
}
Advantages:
Recursion is very simple and easy to understand.
It requires a minimal number of programming statements.
It is used to solve mathematical, trigonometric, or any type of algebraic problems.
It is more useful in multiprogramming and multitasking environments.
Complex tasks can be solved easily.
Disadvantages:
It consumes more storage space .
Compared to other techniques recursion is a time-consuming process and less
efficient.
It is difficult to trace the logic of the function.
Storage Classes in C
Static
Static local variables are visible only to the function or the block in which
they are defined.
A same static variable can be declared many times but can be assigned at
only one time.
Default initial value of the static integral variable is 0 otherwise null.
The visibility of the static global variable is limited to the file in which it
has declared.
The keyword used to define static variable is static.
Example:
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f); }
Output:0 0 0.000000 (null)
Register
The variables defined as the register is allocated the memory into the CPU
registers depending upon the size of the memory remaining in the CPU.
The access time of the register variables is faster than the automatic
variables.
The initial default value of the register local variables is 0.
The register keyword is used for the variable which should be stored in the
CPU register.
Example
Example
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initial
default value of a is 0.
printf("%d",a);
}
Output:0
External
The external storage class is used to tell the compiler that the variable
defined as extern is declared with an external linkage elsewhere in the
program.
The default initial value of external integral type is 0 otherwise null.
An external variable can be declared many times but can be initialized at
only once.
Example:
#include <stdio.h>
int a;
void main()
{
extern int a; // variable a is defined globally, the memory will not be allocated to
a
printf("%d",a);
getch();
}
Output:0