Unit 4 Functions
Unit 4 Functions
What is Function?
• A function is a small independant unit of
program.
No, it does not include the Yes, it includes the full implementation of the
function's body. function.
return_type function_name(parameter_list)
return_type {
function_name(parameter_list); /* body */
}
Typically placed before the function Placed where the function's actual code is
is called. written.
return 0;
}
Call by reference
• In call by reference method address of variable is passed
to the formal arguments of a function.
return 0;
}
Types of functions
• Library functions: Library functions are built in
funtions that are stored in header files that comes
with compiler.
• Library functions can be used whenever
required,but their code is not accessible to
programmer or user.
• Library functions are sometimes called Standard
library functions.
• printf(),scanf(),clrscr(),pow(),sqrt() some commonly
used funtions.
Type refers to datatype of quantity that is written
by the function
User defined Functions
• User defined Functions are functions that are defined by
the programmer or user.
• These functions are not provided by the compiler .
• syntax:
return type Function_Name(argument_list)
{
return value;
}
Adventages of User defined functions
#include <stdio.h>
int add(int,int);
int add(int a, int b)
{
return a + b; OUTPUT:
}
int main() Enter any two numbers: 22 44
{ The sum of 22 and 44 is: 66
int num1, num2, sum;
printf("Enter any two numbers: ");
scanf("%d %d", &num1 , &num2);
sum = add(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2,
sum);
return 0; }
Adventages of User defined function
Method of calling function
Function with No argument No return value