Function
Function
Chapter: 5
Functions
Library Functions:
Library functions are those functions which are already written, compiled and kept within a
library of c language. There are thousands of library functions and when we require call that
function in our program.
User defined function are those function which are created by the user. These are the function
which are written by the user at the time of writing the program.
User defined function mainly consists of the three elements/parts. They are
● Function declaration
● Function definition
● Function call
Examples of user defined functions are
void main(), void addition(), int prime(), int max(int,int)
Function Declaration:
Like variables, function in C must be declared before they are invoked. A function
declaration/prototype consists of four parts.
Programming for Problem Solving (3110003)
examples:
void mul (int, int);
int mul (int, int);
void mul (int a, int b);
void mul ( );
Function Call:
A function can be called using the function name followed by the list of parameters
(arguments) if any and return type as per the declaration of the function.
When a compiler encounter a function call, the control is transferred to the function definition
for execution and then the control is return back to the calling statement.
void function1();
void function2();
main( )
{
……………
……………
function1( );// this is function1 call , so control will be transferred to function1 definition
……………
……………
……………
function2( );// this is function2 call , so control will be transferred to function2 definition
……………
……………
……………
}
Void function1( )
{
……………
……………
……………
} // after executing function1 control will be transferred to calling function ie
function1.
Void function2( )
{
……………
……………
……………
} // after executing function2 control will be transferred to calling function ie
function2.
Function Definition:
● Function header
● Function body
return statements;
}
Example:
Write a c program to find the maximum of two numbers using function.
#include<stdio.h>
{
if(x>y)
return (x);
else
return (y);
}
Actual Argument:
● Actual argument are the arguments which are passed within function call statement.
● Actual argument can be variable name, expression or constants
● Values must be assigned to the variables of actual argument before function call is
made.
Formal Argument:
● Formal argument are the arguments in the function definition. Formal argument must
be a valid variable name. Formal argument receives the value of actual argument.
● If the Actual argument are less than formal argument, the remaining formal argument
will be assigned with garbage values.
● If the actual argument are more than the formal argument, they are discarded.
main( )
{
……………
fun1( a1, a2, a3, ….., an); // this is function1 call , actual argument
……………
}
fun1( b1, b2, b3, ….., bn) // function1 definition , formal argument
{
……………
……………
}
During function call only the copy of values of actual argument is passed, which are received
by the formal argument variable in the function definition. Hence any change made in formal
argument will affect the values of actual argument.
Depending upon the argument passing and returning, the function is classified into four
categories.
Example:
void maximum ( ); // function declaration
void main ( )
{
clrscr ();
maximum ( ); //function call
getch ();
}
Here the function has no argument, hence no parameters are passed while calling but the
function return values, hence return type must be specified in the function( return type is int)
Example:
int maximum ( ); // function declaration
void main ( )
{
int m;
clrscr ();
m=maximum ( ); //function call
printf (“Max=%d”, m);
getch ();
}
else
printf (“Max=%d”, x);
}
Here the function has argument, hence parameter are passed while calling and at the same
time the function return values, hence return type must be specified in the function( return
type is int)
int maximum (int, int); // function declaration
void main ( )
{
int a,b,m;
clrscr ();
printf (“enter the value of a and b”);
scanf (“%d%d”,&a,&b);
m=maximum ( a, b); //function call
printf (“Max=%d”, m);
getch ();
}
Scope of variables :
By the scope of a variable, we mean in what part of the program the variable is accessible is
dependent on where the variable is declared. There are two types of scope for a variable-
Local and Global.
Local variables:
● The variables which are declared inside the body of any function are called as local
variables f or that function.
● These variables can not be accessed outside the function in which they are declared.
Global variables:
● The variables which are declared outside any function definition is called as global
variable.
● These variables are accessible by all the functions in that program.
● i.e. All the functions in program can shared global variable.
Use of local variables advisable Too much use of global variables make
program difficult to debug, so use with care
Call by value
● In call by value , argument values are passed to the function, the contents of actual
parameters are copied into the formal parameters.
● The called function can not change the value of the variables which are passed.
Call by reference
● In call by reference, as the name suggests, the reference of the parameter i.e.
address(pointer) is passed to the function and not the value.
● Call by reference is used whenever we want to change the value of local variables
declared in one function to be changed by other function.
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Output
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
A copy of the value is passed into the An address of value is passed into the
function function
Changes made inside the function is limited Changes made inside the function validate
to the function only. The values of the actual outside of the function also. The values of
parameters do not change by changing the the actual parameters do change by
formal parameters. changing the formal parameters.
Actual and formal arguments are created at Actual and formal arguments are created at
the different memory location the same memory location
The array can be passed to function as argument as like individual variables like a char, an
Integer,a double etc.
Example:
z=max(a,n);
In this function call, a is an array of n integers.
The array a is passed as first argument while the size of the array is passed as second
argument to the function max_n0 is written as flows.
The max() is written as:
int max(int v[], int size)
{
int i,max;
max=v[0];
for(i=0;i<size;i++)
{
if(v[i]>max)
max=v[i];
}
return(max);
}
In above function, the array variable v is not given the size because its Size is the size of the
actual argument (here array a)passed in function call. When array is passed to the
function,actually array elements are not passed,but only the array name which is the address
of the first element in array is passed.
Storage classes:
The storage classes of variable determines the scope and lifetime of a variable.
The variable values can be stored in computer memory or in registers of CPU.
There are 4-storage classes:
1) Auto
2) Register
3) Static
4) Extern
Syntax:
storage_specifiers data_type variable_name;
1)Automatic variable
● It is declare inside the function
● They are created when function is called and destroyed automaticallywhen the
function exited.
● It is referred local or internal variable
● We can define automatic variable using ‘auto’ keyword.
2)Register variable
● A value of variable should be kept into register, instead of memory.
● Register access is much faster than memory access.
● We can define register variable using ‘register’ keyword.
3) Static variable
● The value of variable persists until the end of program.
● A static variable is initialized only once, when the program is complied.
● We can define static variable using ‘static’ keyword.
○ Internal static variable:
■ The value of variable extends up to the end of function.
○ External static variable:
■ External static variable declared outside of all functions and is
variable to all the functions in the program.
4) External variable:
• External variable declared outside the function.
Preprocessor:
The C Preprocessor is a macro Preprocessor (allows you to define Macros) that transforms
your program before it is compiled.these transformations can be inclusion of header file,
macro expansions etc.
All pre processing directives begins with a # symbol.
Example:
#define PI 3.14
Directive Function
Macro substitution:
Macro substitution has a name and replacement text, defined with #define directive. The
Preprocessor simply replaces the name of macro with replacement text from the place where
the macro is defined in the source code.
Syntax:
#define identifier string
Example:
#define pie 3.14
#define count 100
#define capital “Delhi”
File inclusion :
File inclusive Directories are used to include user define header file inside C Program.File
inclusive directory checks included header file inside same directory (if path is not
mentioned).File inclusive directives begins with #include.
Syntax:
#include<filename>
#include “filename”
Example:
#include<stdio.h>
#include<string.h>
#include”test.c”