CP Unit Iv

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

UNIT-IV

Modular Programming: All the high level languages are procedural languages, allow us to
write the programs using set of functions (predefined and user defined). For developing any
software or program or product, first phase user has to understand the problem clearly then
might be divide the problem into simple and understandable sub problems(i.e.
subprograms).Second phase, all the subprgrams are developed independently and tested. At final
stage all these are integrated named as a product or software.

software

subsoftware subsoftwrae

Figure 1.1 concept of Module

Module: Each sub software is known as module. It refers to unit or part of whole. All these
subsoftwares are different role or behavior is known as function (i.e behavior of module).

User defined functions: Every C-program consists of one main function along with predefined
(scanf,printf and so on) and user defined functions. The communication is passed through the
main function. But no communication is present between function1,function2 and function 3.

main()

Function1() Function2() Function3()

Advantages of functions:

 Complex problem can be divided into simple and understandable parts.


 Reuse of code
 Data protection

GT/CP/UNIT-IV/1
Function and Parameter Declarations: A function is a group of statements that together
perform a task. Every C program has at least one function, which is main() and optionally some
predefined and user defined functions.

Requirements for user defined functions: It refers to necessary specifications or aspects of user
defined function as follows.

a. Function prototype or declaration:a function prototype merely specifies its interface,


i.e. what data types go in and come out of it.It tells the compiler about the function name,
its return type and the parameters if passed to the function.it has following syntax

return data type function name(type of data);

for example name of the function to be given as sum, takes the integer values as input and
returns integer as output, then user can prototype the function as following manner.

int sum(int, int);

b. Function calling: It refers to statement inside the main() function consists of function
name with or without augments. Syntax as follows
Function name (arguments or without arguments).
For example function sum is called by sum(x, y);
c. Function definition: It contains a code needed to compute a task. It has header and
body.syntax as follows

Function header
{
Statement1;
--------------;
Statement n;
return (value or expression);
}

GT/CP/UNIT-IV/2
For example function sum can be defined as follows

int sum(int x, int y)


{
int z;
z=x+y;
return z;
}

Types of functions: Functions are categorized into four types based on its return type and
arguments.

1. Void function without parameters.


2. Void function with parameters.
3. Return type(non void) without parameters
4. Return type with parameters.

1. Void function without parameters: a function does not return any value is known as void
function and without parameters indicate does not receive any parameters. Void function does
not return any value, it can be used as statement. Following program shows the usage of void
function without parameters.

#include<stdio.h> //function definition


void greetings(void)
void greetings(void); // function Declaration
int main() {
printf(“Hello World”);
{
return;
greetings(); //function call }
return 0;
}

Output: Hello World

GT/CP/UNIT-IV/3
2. Void function with parameters: another type of function can take or receive parameters but
still returns nothing to calling function.Following program shows the usage of void function with
parameters.

#include<stdio.h> // function definition


void sum(int x, int y)
void sum(int, int); // function Declaration
int main() {
int z;
{
z=x+y;
sum(3, 6); //function call printf(“%d”,z);
return;
return 0;
}
}
Output: 9

3. Return type(non void) without parameters: some functions return a value but do not have any
parameters. The most common use for this design reads data from the keyboard or file and
returns the data to the calling function.It returns a value, it can be used as expression. Following
program shows the usage of return function without parameters.

#include<stdio.h> // function definition


int sum(void); // function Declaration
int sum(void)
int main()
{
{
int x=5, y=10;
int result; return (x+y);
}
result=sum(); //function call
printf(“%d”, result);
return 0;
}
Output: 15

4. Return type(non void) with parameters: some functions that passes parameters and return a
value. Data parameters from calling function to called function and finally returns a value to
calling function.It returns a value,it can be used as expression. Following program shows the
usage of return function with parameters.

GT/CP/UNIT-IV/4
#include<stdio.h> // function definition
int sum(int, int); // function Declaration
int sum(int x, int y)
int main()
{
{
return (x+y);
int result; }
result=sum(5, 15); //function call
printf(“%d”,result);
return 0;
}
Output: 20

Variable Scope: function contains one or more variables and these are defined inside or outside.
scope of variable refers to section or extent of area of the program where the variable is valid. A
variable can have local scope or global scope.

Local variable: Refers to variables are created inside a function are available only to function
itself.

Global variable: Refers to variables are created outside a function and its scope is valid for all
the functions. Following program shows a usage of global and local variable.

#include<stdio.h>
int a=10;// Global variable
void print(int);
main()
{
printf(“%d”,a);
print(5);
}

void print(int b) scope


{

float k=1.5;//Local variable


printf(“%d”,(a+k*b));
}
Output: 10
17

GT/CP/UNIT-IV/5
Global variable a can be accessed in main and print function, where as local variable k accessed
only in print function.

Variable Storage Class: when variable is declared compiler creates a space and length of time
that storage location a are reserved for a variable. All variables storage locations are released
back to the operating system when program is finished running. For example char a , compiler
allocates space for character data type variable a after finish the program space will be
handover to operating system.

Storage class of variable can be defined as where and how long variable storage locations are
kept before they are released. It can be classified into four types such as auto, register, static and
extern.

Local variable storage class: local variable can only be members of the auto, register or static
storage class.

Auto storage class: by default all local variables belong to auto storage class. Syntax of this
storage is name auto must be placed before the variable data type in declaration statement.(i.e
auto int x). When local variable is defined as auto storage class, when function returns calling
function, its local automatic variables will die, that is storage variable is released back to
operating system.

Following program shows working of auto storage class.

#include <stdio.h>
void fun1();
RAM
int main()
{
inti;
for(i=0;i<=1;i++)
{
fun1();
printf("\n");
}
return 0;
}

void fun1()

GT/CP/UNIT-IV/6
{
auto int c=0;
printf("%d",c);
c++;
}

Output: 0
0

Each iteration fun1() called , variable created once function returns to main(), variable is
destroyed along with any value stored in c.

Register storage class: In general the variables are created in RAM .But if the variable is register
storage class, space is created in CPU. So access is very fast compared to auto storage class.
Following example shows a working of register storage class.

#include <stdio.h>
void fun1();
CPU
int main()
{
inti;
for(i=0;i<=1;i++)
{
fun1();
printf("\n");
}
return 0;
}

void fun1()
{
register int c=0;
printf("%d",c);
c++;
}

Output: 0
0

GT/CP/UNIT-IV/7
Static storage class: when the variable is crated and initial value placed in it. There after the
value in the variable is kept without further initialization each time the function is called. Syntax
of static storage class is

Static float a;

Following program shows working of static storage class.

#include <stdio.h>
void fun1();
RAM
int main()
{
inti;
for(i=0;i<=1;i++)
{
fun1();
printf("\n");
}
return 0;
}

void fun1()
{
static int c=0;
printf("%d",c);
c++;
}

Output: 0
1

Global variable storage class: variable is existed until it is finished executing. These values are
not reinitialized again when each time function called. So global variables cannot be declared as
members of auto or register storage classes, which are created and destroyed as program is
executing. So global variables are declared as either static or extern storage.

Static storage class: by default all global variables are static storage class. These variables are
created and destroyed only after finishing the program execution (each time not reinitialized).
The following program shows work flow of static storage class.

GT/CP/UNIT-IV/8
#include <stdio.h>
static int c=0;
void fun1(); RAM

int main()
{
int i;
for(i=0;i<=1;i++)
{
fun1();
printf("\n");
}
return 0;
}

void fun1()
{
printf("%d",c);
c++;
}

Output: 0
1

Extern storage class: an extern declaration statement simply informs the compiler that the
variable already exists and can now can be used. Initialization of the global variable can of
course be made with the original declaration of the global variable. Following program shows
scope of global variable declared in one source code file into another source code file.

//test1.c
//test2.c
#include <stdio.h>
static int c=0; extern int c=0;
void fun1();
void fun1()
int main() {
{ printf("%d",c);
int i; c++;
}
fun1();

return 0;
}

GT/CP/UNIT-IV/9
Pass by value (call by value): In normal course of operation a called function receives values
from its calling function. for example method of calling function and passing values to it is
refereed as function pass by value. Syntax as follows

sum(4, 5);

Pass by Reference (call by reference): Refers to called function receives address from calling
function. Syntax as follows.

sum(&x, &y);

Storing address: when variable are created in memory each of having own name and address.
Its address can be stored in another variable as follows

y=&x;

y is the variable that stores the address of variable x, is known as pointer variable.

Using address: to use stored address c provides us with an indirection unary operator * when
immediately by pointer means means a variable whose address is stored in. *y means the
variable whose address is stored in y.

5 165789

x variable name y
165789 address

Declaring and Using Pointers: like other variable pointer must be declared before they can be
used. If the address of the pointer y is the address of integer, the correct declaration for the
pointer is

int *y;

GT/CP/UNIT-IV/10
Following example shows usage of pointer.

main()
{
int x=10;
int *y;
y=&x;
printf(“%d”,x);
printf(“%u”,y);
printf(“%d”,*y);
}
Output: 10
189456
10

Passing Addresses to a Function: Refers to pass an address to function that will correctly receive
and then use the passed address. Following example shows working of passing address to
function.

#include <stdio.h>
sum(int *,int *);
int main(void)
{
int x=2,y=7;
sum(&x,&y);
return 0;
}

sum(int *a,int *b)


{
printf("%d\n",*a+*b);
}

Output:10

GT/CP/UNIT-IV/11
Swapping and its application: Refers to mutually exchanging the values of the variables. usually
this is done with the data in memory. If x=1 and y=0 after swa(x,y), x will contain 0 and y
contains 1.

#include<stdio.h>

float swap(float *,float *);

int main()

float num1=5.0, num2=8.0;

swap(&num1,&num2);

printf("%f\n",num1);

printf("%f",num2);

return 0;

float swap( float *x, float *y)

float temp;

temp=*x;

*x=*y;

*y=temp;

Output: 8.000000
5.000000

GT/CP/UNIT-IV/12
Recursion: It is the process of function calls itself directly or indirectly. Method of solving a
problem where the solution depends solutions to smaller instances of the of the same problem.(as
opposed to iteration). For example consider finding the factorial of a number n denoted as n! this
is defined as

0! =1

1! =1*1 = 1*0!

2! =2*1 =2*1!

And so on. The definition for n! can be summarized by the following statements

0!=1;

n!= n*(n-1)! For n>=1;

Following example explains the factorial of given number with and without recursion

//with recursion //without recursion


#include <stdio.h> #include <stdio.h>
int fact(int); int fact(int );
int main(void) int main(void)
{ {
intn,result; intn,result;
printf("Enter the number for factorial"); printf(“Enter the number for factorial”);
scanf("%d",&n); scanf("%d",&n);
result=fact(n); result=fact(n);
printf("%d",result); printf("%d",result);
return 0;
} return 0;
int fact(int x) }
{ int fact(intx)
if(x==0) {
return (1); int m=1,k;
else if(x==0)
return (x*fact(x-1)); {
} return (1);
}
else
{
for(k=1;k<=x;k++)
{
m=m*k;

GT/CP/UNIT-IV/13
}
return (m);
}
}

References:

1. C-Programming by Forouzan
2. ANSI C-programming by Gary J Bronson

GT/CP/UNIT-IV/14

You might also like