Function 6
Function 6
Function
A function is a named block of code that
performs some action or specific task .the
statement written in a function are executed
when it is called by its name.
A function is a group of statements that is
executed when it is called from some point of
the program. Each function has a unique
name. functions are the building blocks of c
programs that encapsulate piece of code to
perform specified operations .
The functions are used to accomplish the
similar kinds of task again and again without
writing the same code again.
The function provide a structured
programming approach .it is modular way
of writing programs .the whole program
logic is divided into a number of smaller
modules or functions.
The format of function
type name ( parameter1, parameter2, ...)
{ statements }
where:
· type is the data type specifier of the data returned by
the function.
· name is the identifier by which it will be possible to
call the function.
· parameters (as many as needed): Each parameter
consists of a data type specifier followed by an
identifier, like any regular variable declaration (for
example: int x) and which acts within the function as a
regular local variable. They allow to pass arguments to
the function when it is called. The different parameters
are separated by commas.
· statements is the function's body. It is a block of
statements surrounded by braces { }.
Importance of function
A program may need to repeat the same
piece of code at various places. It may be
required to perform certain tasks
repeatedly. the program may become very
large if function are not used .the piece of
code that is executed repeatedly is stored
in a separate function. the real reason of
using functions is to divide the program
into different parts. these parts of a
program can be managed easily.
Advantage of function
1.Easier to code: a lengthy program can
be divided into small functions it is
easier to write small functions instead of
writing on long program .a function is
written to solve a particular problem.
2.Easir to modify: each function has a
unique name and is written as an
independent block . If there is any error
in the program the change is made to
particular function in which error exists.
A small function is easier to modify than
a large program.
3.Reusability : the code written in functions can
be reused as and when required.
4. a program may consist of many functions
these functions are written as independent
programs. different programmers can work on
different functions at the same time it takes
far less time to complete the program.
Types of functions
User defined functions
A type of function written by programmer
is known as user defined function.
Built-in functions
A type of function that is available as a
part of language is known as built-in
function or library function . These
functions are ready made programs and
these functions are stored in different
header files .built-in function make
program easer and faster to write.
User defined function
A user defined function consists of the
followings
Function declaration
Function definition
Function declaration or function prototype
}
Scope of function
The area in which a function can be
accessed is known as the scope of a
function.
Global function
A type of function that is declared outside
of any function is called global function.
Which can be
Called from any part of program.
Local function
A type of function that is declared within
another function is called local function.
A local function can be called within the
function.
C program is a collection of one or more functions.
A function gets called when the function name is followed by a
semicolon. For example,
main( )
{
argentina( ) ;
}
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 ;
}
A function can be called any number of times.
For example,
main( )
{
message( ) ;
message( ) ;
}
message( )
{
printf ( "\nJewel Thief!!" ) ;
}
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,
main( )
{
message1( ) ;
message2( ) ;
}
message2( )
{
printf ( "\nBut the butter was bitter" ) ;
}
message1( )
{
printf ( "\nMary bought some butter" ) ;
}
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 be called from other function, but a
function cannot be defined in another function. Thus,
the following program code would be wrong, since
argentina( ) is being defined inside another function,
main( ).
main( )
{
printf ( "\nI am in main" ) ;
argentina( )
{
printf ( "\nI am in argentina" ) ;
}
}
#include <stdio.h>
#include<conio.h>
void show(void);
void display(void);
main()
{
show();
display();
show();
}
void show(void)
{ ();
printf("This is show function \n");
display();
}
void display()
{
printf("This is diplay function \n");
}
Passing parameters to
function
Parameters are the values that are provided to
a function when the function is
called .parameter are given in the parenthesis.
The sequence and type of parameter in
function call must be same as the sequence
and type of parameters in function
declaration .
Parameters in function call are called actual
parameter
Parameters in called function are called formal
parameters.
When a function call is executed the value of
actual parameters are copied to formal
parameters.
Pass by value
A parameter passing mechanism in which
the value of actual parameters is copied to
formal parameters of called function is
known as pass by value. If the function
makes any changes in formal parameter it
does not affect the value of actual
parameters .it is the default mechanism for
passing parameters to functions.
#include<stdio.h>
#include<conio.h>
void show();
void main()
{
Printf(“the calling of function show \n“);
show();
getch();
}
void show()
{
Printf(“this is show function”);
}
example
#include <stdio.h>
void sum();
void add(int,char,int);
main()
{
int a,b;
char opr;
printf(" enter no opr and no");
scanf("%d %c %d",&a,&opr,&b);
sum();
add(a,opr,b);
add(12,'-',2);
}
Cont…
sum()
{
int a,b,res;
printf("Enter two nos");
scanf("%d %d",&a,&b);
res=a+b;
printf("res=%d",res);
add(res,'-',30);
}
return 0;
}
This example explain that we input two number in main function pass these
number to a function and the function display the maximum number .
Write a program that inputs a number in main function and passes the number to a
function the function displays table of that number
#include<stdio.h>
#include<conio.h>
table(no);
return 0;
}
void table(int n)
{
int a;
for( a=1;a<=10;a++)
void number(int );
int main()
{
int num;
printf("Enter any no");
scanf("%d",&num);
number(num);
}
void number(int a)
{
int p,n;
p=a-1;
n=a+1;
int main()
{
int value1 = 5, value2 = 10;
return 0;
}