Unit 1
Unit 1
Unit – 1
What is a function?
Types of functions in C
1. Library function
2. User defined function
Library functions
Functions are included with C compilers are known as library functions. These functions are
built-in, pre-compiled and ready to use.
Functions such as printf(), scanf(), pow(), sqrt() etc. are part of C standard library functions.
These functions are defined in C header files. We include these header files in our program as
per our need.
The user can define any number of functions depending on the need.
Return_type function_name(arguments)
//Body of a function
Example:
void sum()
Page 1
:
#include<stdio.h>
#include<conio.h>
void main()
int a, b, max;
clrscr();
scanf(“%d”, &a);
scanf(“%d”, &b);
getche();
Page 2
{
if ( x > y )
else
Function prototype: The declaration of a function before main() function is a function prototype. In above
program statement - void max(int x, int y); is a function prototype.
Function definition: It includes function declarator (or prototype) and function body.
Function parameters: The parameters specified in the function call are known as actual parameter. The
parameters specified in the calling function are known as formal parameter. In above example, the
parameters a and b are actual parameter. The parameters x and y are formal parameter. The scope of
formal parameter is limited to its function only.
Function call: A function call is specified by the function name followed by the arguments enclosed in
parentheses and terminated by a semicolon.
return statement:
- A function that returns something is indicated by the return data types instead of void.
- When return statement found, then execution control will be return to the calling function.
#include<stdio.h>
#include<conio.h>
Page 3
int max(int x, int y);
void main()
{ int a, b, c, max;
clrscr();
printf(“Enter a…”);
scanf(“%d”,&a);
printf(“Enter b…”);
sacnf(%d”, &b);
c = max(a, b);
getche();
{ if ( x > y )
return x;
else
return y;
In above example, return x; and return y; are return statements. Its value is stored in variable c which is in
main function.
The limitation of return statement is that it can be return only one value from a function.
Page 4
Need of Functions:
- Modular programming
- Reduction in the amount of work and development time
- Program and function debugging is easier.
- Division of work is simplified due to the use of divide-and-conquer principle.
- Reduction in size of the program due to code reusability
- Function can be access repeatedly without redevelopment, which in turn promotes reuse of code.
- Library of function can be implemented by combining well designed, tested and proven function.
Categories of a Function:
- This type of function does not receive any argument as well as does not return any value.
- This type of function does not communicate with the caller function.
- It works as an independent working block.
Syntax:
The syntax of a function without argument, no return statement is …
void function_name( )
{
// Function body
}
Page 5
#include<conio.h>
void sum( )
c = a + b;
void main( )
clrscr( );
sum( );
getch( );
Output:
sum = 8
- In above example, the function sum( ) is declared. This function does not contain argument and
return data type.
- This type of function does not receive any argument but it returns a value.
- This type of function communicates with the caller function by returning value back to
the caller.
Syntax:
The syntax of a function without argument, with return statement is …
return_type function_name( )
{
Page 6
// Function body
return some_value;
}
#include<conio.h>
int sum( )
c = a + b;
return c;
void main( )
int c ;
clrscr( );
c = sum( );
getch( );
Output:
sum = 8
- In above example, the function sum( ) is declared. This function does not contain argument but it
returns summation of two numbers ( a and b).
[ 3 ]. Function with argument, No return statement:
Page 7
- This type of function receives an argument but does not return any value.
- For this type of function you must define function return type as void.
Syntax:
The syntax of a function with argument, No return statement is …
#include<conio.h>
int c ;
c = a + b;
void main( )
clrscr( );
sum( a, b);
getch( );
Output:
sum = 8
Page 8
- In above example, the function sum( ) is declared. This function contains two integer arguments
(a and b).
[ 4 ]. Function with argument, with return statement:
return some_variable;
}
#include<conio.h>
int c ;
c = a + b;
return c;
void main( )
int c;
clrscr( );
Page 9
c = sum( );
getch( );
Output:
sum = 8
- In above example, the function sum( ) is declared. This function contains two integer arguments
(a and b) and return its summation.
------X-----
Page 10