0% found this document useful (0 votes)
10 views12 pages

C MODULE 4

The document provides an overview of functions in C, detailing the types of functions (library and user-defined), their advantages and limitations, and the syntax for creating and calling functions. It also covers recursion, its advantages and disadvantages, and the different storage classes in C, including automatic, static, register, and external, along with their characteristics. Additionally, it includes examples to illustrate the concepts discussed.

Uploaded by

krupamariam008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

C MODULE 4

The document provides an overview of functions in C, detailing the types of functions (library and user-defined), their advantages and limitations, and the syntax for creating and calling functions. It also covers recursion, its advantages and disadvantages, and the different storage classes in C, including automatic, static, register, and external, along with their characteristics. Additionally, it includes examples to illustrate the concepts discussed.

Uploaded by

krupamariam008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

Limitations of Library function


1. All predefined function are contained limited task only that is for
what purpose function is designed for same purpose it should be
used.
2. As a programmer we do not having any controls on predefined
function
User Defined Functions

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

 By using functions, we can avoid rewriting same logic/code again and


again in a program.
 We can call C functions any number of times in a program and from
any place in a program.
 We can track a large C program easily when it is divided into multiple
functions.
 Reusability is the main achievement of C functions.
However, Function calling is always a overhead in a C program.
There are three aspects of a C function.

1. Function declaration A function must be declared globally in a c program


to tell the compiler about the function name, function parameters, and
return type.
2. Function call Function can be called from anywhere in the program. The
parameter list must not differ in function calling and function declaration.
We must pass the same number of functions as it is declared in the function
declaration.
3. Function definition It contains the actual statements which are to be
executed. It is the most important aspect to which the control comes when
the function is called. Here, we must notice that only one value can be
returned from the function.

The syntax of creating function in c language is given below:

C function aspects Syntax


1 Function declaration return_typefunction_name (argument list);
2 Function call function_name (argument_list)
3 Function definitionreturn_typefunction_name (argument list) {function
Types of Function According to Arguments and Return Value
Functions can be differentiated into 4 types according to the arguments passed
and value returns these are:
1. Function with arguments and return value
2. Function with arguments and no return value
3. Function with no arguments and with return value
4. Function with no arguments and no return value
1. Function with arguments and return value
Syntax:
Function declaration : int function ( int );
Function call : function( x );
Function definition:
int function( int x )

{
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 for Function with argument and without return value


#include<stdio.h>
void 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);
sum(a,b); }
void sum(int a, int b)
{ printf("\nThe sum is %d",a+b); getch(); }
3. Function with no argument and no return value
When a function has no arguments, it does not receive any data from the calling
function. Similarly, when it does not return a value, the calling function does not
receive any data from the called function.
Syntax:
Function declaration : void function();
Function call : function();
Function definition :
void function()
{ 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

Storage classes in C are used to determine the lifetime, visibility, memory


location, and initial value of a variable. There are four types of storage classes in
C:Automatic,External,Static,Register.

Storage Storage Default Scope Lifetime


Classes Place Value

auto RAM Garbage Local Within function


Value

extern RAM Zero Global Till the end of the main


program Maybe declared
anywhere in the program

static RAM Zero Local Till the end of the main


program, Retains value
between multiple
functions call

register Register Garbage Local Within the function


Value

1. Scope is defined as the availability of a variable inside a program, scope is


basically the region of code in which a variable is available to use.
2. Visibility of a variable is defined as if a variable is accessible or not inside
a particular region of code or the whole program.
3. Lifetime of a variable is the time for which the variable is taking up a valid
space in the system's memory.
Automatic
 The visibility of the automatic variables is limited to the block in which
they are defined.
 The scope of the automatic variables is limited to the block in which they
are defined.
 The automatic variables are initialized to garbage by default.
 Every local variable is automatic in C by default.
Example
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables
a, b, and c.
return 0;
}
Run Code >>
The automatic variables 'a' (int), 'b' (char), & 'c' (float) are declared in this piece
of C code in the C Compiler without being initialized. Then, because they are
uninitialized, it prints their initial default values, which may result in behavior
that is not defined.

Output:garbage garbage garbage

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

You might also like