Sunteți pe pagina 1din 17

Functions and Structures

PIC Chapter No 4
Made by- Prof.U.N.Abhonkar
Function Function
A number of statements grouped together into a single
logical unit is referred to as function. logical unit is referred to as function.
Function is a self contained block of statements or
subprogram or set of instructions written to do a
particular task
Example,
1. A function for addition of two numbers.
2. main() function in C program
Need of function Need of function
Writing functions avoid rewriting same code over and
over over
The programs are of large size and are more complex,
due to which the modification and maintenance becomes
difficult
Separating the code into functions also makes the
i d i d d d program easier to design and understand
Function reduces size of the program
How to use functions in the program How to use functions in the program
While using function in the program, three steps are to
used, used,
1. Function declaration
2. Function call
3. Function definition
Function Declaration Function Declaration
Function declaration is similar to variable declaration. Like
when we need any variable in program, we declare those when we need any variable in program, we declare those
variables
In the same way if we need function, we have to declare
them
The function can be declared above the main() or inside
i () main()
Function declared above main() is called global function
F ti d l d i id i () i ll d l l f ti Function declared inside main() is called local function
Function Declaration: Function Declaration:
Declaration tells information about function to the
compiler compiler
Syntax:
return type functionname ( data types of parameters);
Ex, Declaration of function to add two numbers
int add ( int , int );
above statement declares a function add() which takes two
integer values as parameters and returns integer addition
Function Call Function Call
Function call is calling a function in program by writing
function name and by passing actual arguments to the function y p g g
In function call, we use the function by passing actual values.
Syntax:
functionname ( actual values);
Ex, calling the function for addition of two numbers
add ( 4 , 7);
above statement calls a function add() and passes two integer
values 4 and 7.
Function Definition Function Definition
Function definition consists of logic of function
Syntax:
return type functionname ( data types of parameters) yp ( yp p )
{
statements;
return value;
}
Ex function definition of a function to add two numbers
}
Ex, function definition of a function to add two numbers
here x and y are
temporary variables to
int add ( int x , int y)
{
above code is the definition of add() function
p y
hold values passed at the
time of calling

{
return (x+y);
}
above code is the definition of add() function.
#include<stdio.h>
#include<conio.h>
void main()
{
int add(int , int),sum;
Function declaration
( )
clrscr();
sum = add(4,7);
Function call
printf(Addition is %d,sum);
getch();
}
int add ( int x , int y)
{
Function definition
{
return ( x + y );
}
Function definition
Call By Value Call By Value
When we pass actual values at the time of function call, it
is called as call by value / parameter passing by value is called as call by value / parameter passing by value
Ex,
add ( 4 , 7);
here actual values 4 and 7 are passed at the time of
calling add() function
#include<stdio.h>
#include<conio.h>
void main()
{{
int add(int , int),sum;
clrscr();
sum = add(4,7);
printf(Addition is %d,sum);
getch();
}
int add ( int x , int y) int add ( int x , int y)
{
return ( x + y );
}
Call By Reference Call By Reference
When we pass references / addresses of variables at the
time of function call, it is called as call by reference time of function call, it is called as call by reference
Ex,
add ( &n1 , &n2);
Here addresses of variables n1 and n2 are passed
at the time of calling add() function
#include<stdio.h>
#include<conio.h>
void main()
{{
int add(int * , int *),sum,n1=4,n2=7;
clrscr();
sum = add(&n1,&n2);
printf(Addition is %d,sum);
getch();
}
int add ( int *x , int *y) int add ( int x , int y)
{
return ( *x + *y );
}
Recursion Recursion
Expressing an entity in terms of itself is called recursion Expressing an entity in terms of itself is called recursion
Recursion functions are those in which there is at least
one function call to itself.
When the function call itself in its own body/ definition is
called recursion.
Calling a function inside definition of same function is
called as recursion.
// program to calculate factorial of any given number using recursion
#include<stdio.h>
#include<conio.h>
id i () void main()
{ long int fact(int),f;
int n;
clrscr(); ()
printf(Enter any number);
scanf(%d,n);
f = fact(n);
printf(Factorial of given number is %d f); printf( Factorial of given number is %d ,f);
getch();
}
long int fact( int x)
{ long int y;
if(x==1)
{
return 1; return 1;
}
else
{
* f ( 1) y = x * fact( x - 1);
}
}
Structure Structure
Structure is a collection of variables possibly having
different data types different data types
Declaration of structure:
struct tagname g
{
data type member1 ;
data type member2 ;
Ex,
-----------------------------
} ;
struct student
{
int rollno;
char name[20]; char name[20];
float per;
} ;
Structure variables Structure variables
Declaration of structure variables
1
struct student
1.
struct student
{
int rollno;
char name[20];
2.
float per;
} s ;
2.
struct student
{
int rollno;
h [20] char name[20];
float per;
} ;
struct student s; st uct stu e t s;
Maintaining structure data Maintaining structure data
#include<stdio.h>
#include<conio.h>
struct student struct student
{
int rollno;
char name[20];
float per;
};
void main()
{ struct student s; { struct student s;
clrscr();
printf(\nEnter student information: Roll No, Name and Percentage);
scanf(%d%s%f ,&s.rollno,&s.name,&s.per);
f(\ Th d f ) printf(\n The student information is:);
printf(\nStudent Roll No = %d, Name = %s, Percentage= %f ,s.rollno,s.name,s.per);
getch();
}}
Array of structure Array of structure
#include<stdio.h>
#include<conio.h>
struct student st uct stu e t
{ int rollno;
char name[20];
float per;
}};
void main()
{ struct student s[4];
int i;
clrscr();
for(i=0;i<=3;i++)
{ printf(\nEnter student information: Roll No, Name and Percentage);
scanf(%d%s%f &s rollno &s name &s per); scanf( %d%s%f ,&s.rollno,&s.name,&s.per);
}
printf(\n The student information is:);
for(i=0;i<=3;i++)
{ printf(\nStudent Roll No = %d, Name = %s, Percentage= %f ,s.rollno,s.name,s.per);
getch();
}

S-ar putea să vă placă și