CFP - Unit 5
CFP - Unit 5
UNIT 5
arrays.
struct book
{
char name[10] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
Note the following points while declaring
a structure type:
is followed by a semicolon.
For example,
main( ) {
argentina( ) ;
}
Continue
A function is defined when function name is
followed by a pair of braces in which one or
more statements may be present.
For example,
argentina( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
Continue
Any function can be called from any other function.
Even main( ) can be called from other functions.
For example,
main( )
{
message( ) ;
}
message( )
{
printf( "\n Can't imagine life without C" ) ;
main( ) ;
A function can be called any number of
times.
For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf( "\n Jewel Thief!!" ) ;
}
Function Order
The order in which the functions are defined in a program and the
order in which they get called need not necessarily be same.
For example,
void main( )
{
message1( ) ;
message2( ) ;
}
message2( )
{
printf( "\n But the butter was bitter" ) ;
}
message1( )
{
printf( "\n Mary bought some butter" ) ;
}
Continue
Here, even though message1( ) is getting
called before message2( ), still, message1( )
has been defined after message2( ).
However, it is advisable to define the
functions in the same order in which they are
called. This makes the program easier to
understand.
A function can call itself. Such a process is
called ‘recursion’.
Continue
A function can be called from other function, but a function
cannot be defined in another function.
Thus, the following program code would be wrong, since
main( )
{
printf( "\nIam in main" ) ;
argentina( )
{
printf( "\nIam in argentina" ) ;
}
}
Continue
There are basically two types of functions:
1. Standard Functions / Built-in Functions or
Library functions
Defined in C programming Libraries/Header Files
,
Ex. printf( ), scanf( ) etc.
2. User-defined functions
User can define functions in C program as per
his/her need .
E.g. to draw a circle , user can define c function
as drawcircle()
Three Aspects of
Function
Function Declaration, Function Call and Function
Definition
Function Declaration or Prototype – This informs compiler
executed.
Functions Aspects Syntax
Function Declaration return type function_name (argument list);
Function Call function_name (arguments list);
Function Definition return_type function_name (arguments
list) {
Body of function; }
Why Use Functions?
Why write separate functions at all?
Why not squeeze the entire logic into one function,
main( )?
Two reasons:
1. Writing functions avoids rewriting the same code over
and over. Suppose you have a section of code in your
program that calculates area of a triangle. If later in
the program you want to calculate the area of a
different triangle, you won’t like it if you are required
to write the same instructions all over again. Instead,
you would prefer to jump to a ‘section of code’ that
calculates area and then jump back to the place from
where you left off. This section of code is nothing but
Continue….
2. Using functions it becomes easier to write programs
and keep track of what they are doing. If the operation
of a program can be divided into separate activities,
and each activity placed in a different function, then
each could be written and checked more or less
independently. Separating the code into modular
functions also makes the program easier to design
and understand
3. The core concept of C functions are:
Re-usability,
functionality &
To improve understandability of very large C
Example
#include<stdio.h> /*Program illustrates function aspects */
float squareNo( floatx );/*function Prototype, also
called function Declaration*/
void main( )/* main function, program starts from
here*/
{
float m, n ;
printf( "\nEnter some number for finding square \n");
scanf( "%f", &m ) ;
n = squareNo( m ) ;/*Function Call*/
printf( "\n Square of the given number %f is %f",m,n);
getch( );
}
float squareNo( floatx )/* function
Definition*/
{
float p ;
p=x*x;
return( p ) ;
}
Continue
There are two ways that a C function can be
called from a program. They are,
1. Call by Value
2. Call by Reference
Call by Value
In call by value method, the value of the variable
is passed to the function as parameter.
The value of the actual parameter can not be
modified by formal parameter.
Different Memory is allocated for both actual and
formal parameters. Because, value of actual
parameter is copied to formal parameter.
Note:
Actual parameter :- This is the argument which is
used in function call.
Formal parameter:- This is the argument which is
used in function definition
EXAMPLE :
USING CALL BY VALUE In this program, the
values of the variables “m” and “n” are
passed to the function “swap”. These values
are copied to formal parameters “a” and “b”
in swap function and used.
SWAPPING
#include<stdio.h>
void swap(inta, intb);/ *function Prototype, also called function
Declaration*/
void main()
{
int m = 22, n = 44;
printf(“ \n values before swap: %d %d", m, n);
swap(m, n);/*calling function by value*/
getch();
}
void swap(int a, int b) /*Function Definition*/
{
int tmp;
tmp= a;
a = b;
b = tmp;
printf(" \n values after swap : %d %d", a, b);
Call by Reference
In call by reference method, the address of
the variable is passed to the function as
parameter.
The value of the actual parameter can be
modified by formal parameter.
Same memory is used for both actual and
formal parameters since only address is used
by both parameters
EXAMPLE
USING CALL BY REFERENCE In this program,
the address of the variables “m” and “n” are
passed to the function “swap”
fun( )
{
char ch;
printf( "\n Enter any alphabet " ) ;
scanf( "%c", &ch) ;
if ( ch>= 65 && ch<= 90 )
return ( ch) ;
else
return ( ch+ 32 ) ;
Continue
All the following are valid return statements.
return ( a ) ;
return ( 23 ) ;
return ( 12.34 ) ;
return ;
In the last statement a garbage value is returned to the calling
function since we are not returning any specific value.
If we want that a called function should not return any value, in
that case, we must mention so by using the keyword void as
shown below.
void display( )
{
printf( "\n Heads I win..." ) ;
printf( "\n Tails you lose" ) ;
Continue
A function can return only one value at a time.
Thus, the following statements are invalid.
return ( a, b ) ;
return ( x, 12 ) ;
Continue
If the value of a formal argument is changed in the called function,
the corresponding change does not take place in the calling
function.
main()
{ OUTPUT:
inta=30; 60
fun(a); 30
printf("\n%d",a);
}
fun(intb)
{
b=60;
printf("\n%d",b);
THANK YOU