0% found this document useful (0 votes)
20 views

C Short Notes Module 4

User defined functions allow programmers to create their own functions to organize code into logical units. The main() function is called automatically when a C program begins execution. Other user defined functions are called from main() or other functions. Functions make code more modular and easier to debug. User defined functions differ from library functions which are pre-written and stored in libraries. Functions have parameters, a return type, a definition and body, and are called from the main() function or other functions. Variables used in functions can be passed by value or address. Recursion occurs when a function calls itself. Functions and variables have scopes that determine where they can be accessed in a program.

Uploaded by

roshni varghese
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Short Notes Module 4

User defined functions allow programmers to create their own functions to organize code into logical units. The main() function is called automatically when a C program begins execution. Other user defined functions are called from main() or other functions. Functions make code more modular and easier to debug. User defined functions differ from library functions which are pre-written and stored in libraries. Functions have parameters, a return type, a definition and body, and are called from the main() function or other functions. Variables used in functions can be passed by value or address. Recursion occurs when a function calls itself. Functions and variables have scopes that determine where they can be accessed in a program.

Uploaded by

roshni varghese
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C short notes: -Module IV

User defined functions:


A number of statements grouped into a single logical unit is referred to as a function.
A C program is a collection of functions. The function main( ) is a specially recognized function in
C, where it starts its execution. The other functions are executed when the function main( ) calls them
directly or indirectly. The function main( ) is also a user defined function except that the name of the
function, the number of arguments and the argument types are defined by the language.
If a program is divided into functions , the task of debugging, testing and maintaining becomes more
easy. Each function can be independently coded and later combined into a single program.
User defined function and Library function
The functions that have already been written, compiled and placed in libraries are called library
functions.
Ex:printf( ),scanf( )
Functions where user has the freedom to choose the function name, return data type, and
arguments(number and type) are called user defined functions.
Ex:#include<stdio.h>
double cube(int );//function declaration
int main( )
{
int v;
double c;
printf(“enter the number/n”);
scanf(“%d”, &v);
c=cube(v);// function call
printf(“The cube of the number is %lf”,c);
return 0;
}
// function definition
double cube(int i)
{
return i* i*i;
}
Different parts of a function
1. function declaration or function prototype
2. function definition
3. function call
4. function return value
5. function parameters or arguments
Function declaration or function prototype:
A function declaration provides the following information to the compiler-
1. The name of the function
2. The type of the value returned
3. The number and type of the arguments that must be supplied in a call to function
When a function call is encountered, the compiler checks the function call with its declaration, so that
correct argument types are used.
syntax:
ret_type function_name(type,type,….type);
ret_type specifies the datatype of the value in the return statement.
ex: double cube(int);
The above declaration informs the compiler that the function cube has one argument of type int and it
returns double value.
If the functions are defined before they are called , then declarations are unnecessary.
Function definition:
The function definition includes the function body. The function body contains the statements that
make up the function delimited by braces.
The first line in the function definition (function declarator) includes the function name, number of
arguments, argument types, and the return type.
ex: double cube(int i)
{
return i*i*i;
}
Function return value:
A function may or may not return value. If a function does not return a value, the return type in the
function definition and declaration is specified as void. Otherwise return type is a valid data type.
ex: // a function that does not return a value
void display(int i)
{
printf(“i= %d”,i);
}
Function call
A function call is specified by the function name followed by the values of parameters enclosed within
parenthesis, terminated by semicolon(;).
int main( )
{
int i,;
double c;
c=cube(i);//………..function call statement
printf(“cube=%lf”,c);
}
The function cube ( ) is called and the return value is assigned to the variable c. The control is
returned to the printf ( ) statement after the statements in function definition is get executed.
Function parameters
Function parameters are the means of communication between the calling and called functions.
They can be classified as formal and actual parameters. The formal parameters are the parameters
given in the function declaration and function definition. The actual parameters, often known as
arguments are specified in the function call.
#include<stdio.h>
int sum(int a, int b)
{
return a+b;
}
void main( )
{
int x,y,z;
z=sum(x,y);
}
The variables a and b are formal parameters in the function definition and the variables x and y are
called actual parameters in the function call. The actual parameters specify the values that are passed
to the function sum.
The following conditions must be satisfied in the function call.
1. The number of arguments in the function call and the first line of the function
definition(function declarator) must be the same.
2. The data type and the order of each arguments in the function call(actual parameters) should
be the same as the corresponding parameters in the function definition (formal parameter).
C provides 2 mechanisms to pass arguments to a function.
1. passing arguments by value
2. passing arguments by address or by pointers

Passing by value: It means the contents of the arguments in the calling function are not
changed, even if they are changed in the called function.
The content of the variable is copied to the formal parameter of the function definition,
preserving the contents of arguments in the calling function.
Passing by address: The address of the variable is passed here instead of the value.
Recursion:
A function that contains a function call to itself is called recursion.
ex:
#include<stdio.h>
//function to find the factorial of a number
int fact(unsinged int num)
{
if(num==0)
return 1;
else
return fact(num-1)*num;
}
void main( )
{
unsigned int n;
printf(“enter the number\n”);
scanf(“%d”,&n);
printf(“The factorial of a number is%d”, fact(n));
}
Two important conditions for recursive function:
1. Each time a function calls itself , it must be closer to the solution
2. There must be a decision criterion for stopping the process of computation.
Recursive and Iterative definition of a function
A function that contains a function call to itself is called recursion.
recursive definition of a function to find the factorial of a number:
ex://function to find the factorial of a number
int fact(unsinged int num)
{
if(num==0)
return 1; //base case
else
return fact(num-1)*num;
}
void main( )
{
unsigned int num;
printf(“enter the number”);
scanf(“%d”, &num);
printf(“The factorial of the number is %d”, fact(num));
}
Iterative definition of function to find factorial of a number:
int fact(unsinged int num)
{
unsigned int i;
int fact=1;
for(i=num;i>1;i--)
{
fact*=i;
}
return fact;
}
void main( )
{
unsigned int num;
printf(“enter the number”);
scanf(“%d”, &num);
printf(“The factorial of the number is %d”, fact(num));
}
Comparison Chart

RECURSION ITERATION

The statement in a body of function calls the Allows the set of instructions to be repeatedly
function itself. executed.

In recursive function, only termination Iteration includes initialization, condition,


condition (base case) is specified. execution of statement within loop and update
(increments and decrements) the control
variable.

A conditional statement is included in the The iteration statement is repeatedly executed


body of the function to force the function to until a certain condition is reached.
return without recursion call being executed.

If the function does not converge to some If the control condition in the iteration
condition called (base case), it leads to statement never become false, it leads to
infinite recursion. infinite iteration.

Scope and Extent of a variable


Extent of the variable(lifetime)
The period of time during which memory is associated with a variable is called the extent of a
variable. Local variables declared in functions, memory is allocated when function starts executing,
and released when the function returns.
void display( )
{
int i;
i=10;
}
The memory space occupied by the variable i is decided when the function is invoked or executed.
Scope of the variable:
The variables declared outside of all functions are called global variables and can be accessed by all
the functions.
The region of source code over which the declaration of an identifier is visible is called the scope of
the identifier.
void display( )
{
int i;
i=10;
}
void main( )
{
i=20;
display( );
}
will give errors as i is not visible in main( ) function.
When both global and local variables have the same name, a statement using that name will access
the local variable.
#include<stdio.h>
int a=10;
void main( )
{
int a=20;
printf(“%d”,a);//prints 20
printf(“%d”, ::a)// prints 10
}
Storage classes
The period of time during which memory is associated with a variable is called the extent of the
variable. The lifetime of a variable is characterized by its storage class.
The storage class of a variable indicates the allocation of storage space to the variable.
Storage classes are four kinds:
1. auto
2. register
3. static
4. extern
auto variables:
All variables declared within a function are auto by default. The extent of a variable is defined by a
scope. Variables declared auto can only be accessed only within the function .
They are created when the function is invoked(start its execution) and destroyed when it is exited.
void display( )
{
int i;
i=30;
}
void main( )
{
display( );
printf(“%d”,i);
}
The execution of this code return compilation error as i is not visible in main( ) function. The variable
i is visible inside the function display( ) only. They are created when the function is called and
destroyed automatically when function is exited .
register variable:
register variable are stored in the registers of the microprocessor, instead of keeping in the memory.
Since a register access is much faster than a memory access, keeping the frequently accessed
variables (ex:loop control variables) in the register will lead to faster execution of the programs.
ex: register int count;
static variables:
The value of the static variable persists until the end of the program. A variable can be declared static
using the keyword static like
static int x;
ex:
static_display( )
{
static int x=0;
x=x+1;
printf(“x=%d”,x);
}
void main ( )
{
int i;
for(i=0;i<3;i++)
{
static_display( );
}
}
output:
x=1
x=2
x=3.
Here, the function static_display( ) is called for three times and the variable x is alive(remain in
existence) throughout the program, hence it can be used to retain the values between the function
calls.
If we declared x as an auto variable, the output will be
x=1
x=1
x=1.
This is because each time static_display( ) is called, the auto variable is initialized to zero. When the
function terminates its value ‘1’ is lost. A static variable is initialized only once, when the program is
compiled. It never initialized again.
Extern variable:
External or global variables is visible only from the point of declaration to the end of the program.
Consider a program segment
void main( )
{
y=5;
…….
}
int y;
func1( )
{
y=y+1;
}
will issue error message by the compiler as the function main( ) is concerned, y is not defined. The
variable y is defined after the function main( ). Hence, the function main( ) cannot access the variable
y. The problem can be solved by declaring the variable with storage class extern.
void main( )
{
extern int y;
……….
}
func1( )
{
extern int y;
………..
}
int y;
Although the variable y has been defined after both the functions, the external declaration of y inside
the functions informs the compiler that y is an integer type defined somewhere else in the program.
In ,multifile programs ,if we want to have one global variable accessible to all functions in these files
the keyword extern is used in the declarations in all these files where it is accessed except one .
file1.c
void main( ) file2.c
{ int m;//global variable
extern int m; func2( )
int i; {
… int i;
} …..
func1( ) }
{ func3( )
int j; {
…. int count;
} ……….
}

You might also like