Concept of function in C programming
A function is a
self-contained block of statements that performs a particular task or job. The C program consist of one or more functions
the execution of each function begins as sequence of function call appears in main()
function. If program contain only one function then it is known as main()
function.
If we declare
function once we can call it from different place many times. After execution
of that function the control will returned to the point from where the function
was called.
Advantage of function
1.
It facilitates top-down programming approach to solve a problem. In this
programming style, the high level logic of overall problem is solved first
while the details of each lower-level function are addressed later.
2. Program development will easier and
faster.
3. Function reduces program complexity
and increase readability.
4. Function
increase code reusability by avoiding rewriting of same code again and again.
5. Length of program can be reduced by
using function at appropriate places.
6. Function helps us to understand
program logic involved in the program.
7. Recursive call is possible through
function.
8. Function can be stored in a library
and reusability can be achieved.
Types of function
In C program we can use two types of function. They
are as:
1.
Library function
2. User defined
function
Library
function
Library function
are also called predefined or built in functions because these functions are
pre-defined, compiled and placed in C-library. We only used these function in our program by
including the respective header files. For example sqrt() is mathematical library function which is used
for finding out square root of any number. The function scanf() and printf() are input/output library function defined in stdio.h
header files.
To used library files in our program we should know:
1. Name of function and its purpose
2. Types and number of arguments it accepts
3. Type of the value it return
4. Header file name to be included.
/* Program to find the square root of any number using library function */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n,s;
clrscr();
printf("enter the number
:");
scanf("%f",&n);
s=sqrt(n);
printf("Square root of = %f is
= %f",n,s);
getch();
}
Output
Enter the number: 16
Square root of =16 is 4.00
User
defined function
Functions which
are developed by the user or programmer at the time of writing program are
known as user defined functions. To
create and use these function, we should know about these three things:
1.
Function definition
2.
Function declaration
3.
Function call
Function definition
Function
definition is model or blueprint of the functions, which provides basic
information about a function which tells the compiler that the function is used
currently or not.
Syntax:
return_type function_name (type1 arg1,
type2 arg2,…………)
{
local variables;
statements;
……………….;
return(expression);
}
return_type :
data type of result (can be omitted, default int),void –indicates no
return values on function.
function_name :
any valid identifier that user can used.
type arg1, type arg1,….:
types and number of arguments that the function passed value from main() to the called function.
Function prototype or function declaration
The function declaration or prototype is model or
blueprint of the function, which provides basic information about a function
which tells the compiler that the function used correctly or not. Prototype only needed if function definition
comes after main() program. The function declaration is same as what we
write on first line in function definition.
Syntax:
return_type function_name (type1 arg1, type2
arg2,…………)
For example:
int product( int a ,int b, int c);
float sum(int x,
int y, int z);
void name(char name[]);
Function call
The function which calls another function is called
calling function. Function call
specifying its name followed by list of arguments enclosed in parentheses and
separated by commas. While calling function we pass actual arguments. Actual
arguments can be written in the form of variable, constants or expressions or
any function call that return a value. The name of the arguments, which are
mentions in the function definition are called formal or dummy
arguments since they are used just to hold the values that are sent by the
calling function.
s=sum (a,b);
printf(“the sum is
=%d”,sum(20,60);
p=product(x+y,z);
Return statement
It
is the jumping statement used to transfer the control from called function to
calling function. It is executed just before the function completes its job.
The job of return statement is to handover a value given by function to the
point from where the call was made.
Syntax:
return;
Note:
Returning control from function that does not return value
or
return
expression;
Note:
The return value could be any valid expression that returns a value. The value
may be:
- a constant
- a variable
- a calculation, for instance (a + b) * c
- call to another function that returns a value
Example:
return
1:
return
x++;
return
(3*sum(a,b));
Example: /*
program to display the sum of first ten natural numbers void
main() { int sum = sumDigits(); printf("%d\n", sum); return; } int
sumDigits() { int sum = 0; int digit; for(digit = 0; digit <= 9; ++digit) { sum += digit; } return sum; |
Explanation: The
function main is called when our program is started. Then it calls the
routine sumDigits. At this point “main” is paused and sumDigits starts. The
second function will do its calculations and reach
the return sum; statement. Here it ends and the control is
transferred back in main. Since sumDigits returns a value(the value of the
sum variable), a value will appear in the place where the function was
called.
The sum of the digits is 1+2+3…+10= 55. This value will appear in the place
of invocation and the variable sum will take this value. Then we print that
value and return from the main function, which ends our program. |
Types of user defined function
The
functions can be divided into four types on the basis of the arguments and
return value;
1. Function with no arguments and no return
value.
2. Function with no argument and a return
value.
3. Function with argument and no return
value.
4. Function with argument and a return
value.
Function with no argument and no
return value.
This
type of function will not return any value to calling function so we use return
type void and it will not also passed value to the called function on
the argument either kept empty or use void. The syntax is as:
void func(void); //return type is void and argument also void
main( )
{
………..
func(
); //function call
……….
}
void func(void) // function defination
{
……….
statement;
……….
}
/* Program of user
define function with no argument and no return value */
#include<stdio.h>
#include<conio.h>
void sum(void);
void main()
{
clrscr();
sum();
getch();
}
void sum(void)
{
int sum, a,b;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum=a+b;
printf("The sum of two number is
=%d",sum);
}
Function with no argument and a
return value.
These
types of function do not receive any arguments but they can return a value.
Here it will not pass the argument but it can return value. So we used int
as the return type it will return integer value. The syntax is as:
int func(void); //function declaration with no argument but
return type
main( )
{
int
r;
………
r
= func( ); //calling function
……..
}
int func(void) //function definition
{
……
……….
return
(expression);
}
/* Program of user
define function with no argument and
return value */
#include<stdio.h>
#include<conio.h>
int sum(void);
void main()
{
clrscr();
printf("The
sum of two number is =%d",sum());
getch();
}
int sum(void)
{
int sum, a,b;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum=a+b;
return sum;
}
Function with argument and no return
value.
This
types of function can pass the argument but will not return the value to the main()
function.
Here
we used void as the return type and value can be pass from main()
function to the called function the syntax is as:
void func(int, int); //function declaration with no return value
but argument
main( )
{
…………..
func(a,
b); // calling function
………..
}
void func(int c, int d) // function definition
{
…..
statements
…………….
}
/* Program of user
define function with argument and
no return value */
#include<conio.h>
void sum(int a,int b);
void main()
{
int a,b;
clrscr();
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
getch();
}
void sum(int a, int b)
{
int s;
s=a+b;
printf("the sum of two number is
=%d",s);
}
Function with argument and a return
value.
These
types of function will pass the value from main program to the function and the
calculated value will be return to the main function. So we use the int as
the return type and pass the argument. The syntax is as:
int func(int, int); /*function
declaration with return type and
argument*/
main( )
{
int
r;
…………..
r
= func(a, b); //calling function
…………….
func(c,
d);
}
int func(int a, int b) //function definition
{
…………..
…………
return(expression);
}
/* Program of user
define function with argument and return value */
#include<conio.h>
int sum(int a,int
b);
void main()
{
int a,b,s;
clrscr();
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
s=sum(a,b);
printf("The sum of two number is
=%d",s);
getch();
}
int sum(int a, int
b)
{
int x;
x=a+b;
return x;
}
Local, global and static variables
Local variables
Local variable is the part or area
of program within which it is accessible or recognized.
For example:
void func();
{
int a, b,c;
………
……..
}
The local variables can be used
only in those function or block, in which they are declared. The same name
variables may be used in different function.
Global variables
Variables declared outside all
functions come under the external storage class. Normally these variables are declared
before the main functions.
int a=10, b=20; // declaration of global variable before
main() function;
void main();
{
Local variable;
………….;
}
Static variables
static
variables
are declared by writing keyword static in front of the declaration
static data_types var_name; |
A static variable is initialized only once
and the value of a static variable is retained between function calls.
If static variables are not initialized then it is automatically
initialized to 0.
/* Program use of local global and static variables */
#include<stdio.h>
#include<conio.h>
int a=8,b=10; //global variable
void display();
void main()
{
int x=9; //local variable
static int z=7; // static variable
printf("the local variable
x=%d and golabl variable a=%d",x,a);
printf("the value of static
variable in main function=%d",z);
getch();
}
void dispaly()
{
printf("the value of a =%d,
and b=%d",a,b);
}
Recursion
A recursive function
is one that calls itself directly or indirectly to solve a smaller version of
its task until a final call which does not require a self-call. Thus, the
functions is called recursive function if it calls to self and recursion is a
process by which functions call itself repeatedly until some specified
condition will be satisfied.
Now we will
write a program, which find out the factorial using a recursive function.
/* Program to find
the factorial of given number*/
#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
int n;
clrscr();
printf("Enter the
number:");
scanf("%d",&n);
printf("The factorial of
given number=%d is= %ld",n,fact(n));
getch();
}
long int factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
This function returns 1 if the argument n is
0, otherwise it return n*factorial(n-1). To return n*factorial(n-1),
the value of factorial(n-1) has to be calculated for which factorial(
) has to be called again but this time with an argument of n-1. This
process of calling factorial( ) continues till it is called with an
argument of 0.
No comments:
Post a Comment