User Defined Function
User Defined Function
User-DefinedFunctions
C functions can be classified into two categories:
a) library functions : library functions are not required to be written by us. for
example main(), printf(),sqrt() etc
b) user-defined functions. a user-defined function has to be developed by the user
at the time of writing a program. A function is a self-contained block of code
that performs a particular task.
A MULTI-FUNCTION PROGRAM
A function is a self-contained block of code that performs a particular task. Once a
function has been designed and packed, the inner details of operation are invisible to the
rest of the program.
Page 1
Programming in C User defined functions
DEFINITION OF FUNCTIONS
A function definition, also known as function implementation shall include the following
elements;
1. function name;
2. function type;
3. list of parameters;
4. local variable declarations;
5. function statements; and
6. a return statement.
All the six elements are grouped into two parts, namely,
• function header (First three elements); and
• function body (Second three elements).
A general format of a function definition:
function_typefunction_name(parameter list)
{
local variable declaration;
executable statementl;
executable statement2;
return statement;
}
⮚ Function Header
The function header consists of three parts: the function type (also known as return
type), the function name and the formal parameter list. A semicolon is not used at the end
of the function header.
● The function typespecifies the type of value (like float or double) that the
function is expected to return to the program calling the function.
- If the return type is not explicitlyspecified, default type will be integer type.
- If the function is not returning anything, then specify as void.
● The function name is any valid C identifier. The name should be appropriate to
the task performed by the function.
● Formal Parameter List :
- The parameter list declares the variables that will receive the data sent by the
calling program.
- They serve as input data to the function.
- The parameter list contains declaration of variables separated by commas and
surrounded by parentheses.
- Examples: float quadratic (int a, int b, int c) {. . . }
double power (double x, int n) {. . . }
Page 2
Programming in C User defined functions
● A function that do not receive anyvalues from the calling program, indicatedas
empty parameter list or use void.
Eg: void printline(void)
{
.....
}
⮚ Function Body
● The function body contains the declarations and statements necessary for
performing the required task.
● The body enclosed in braces, contains three parts,:
Page 3
Programming in C User defined functions
A function may have more than one return statements. This situation arises when the
valuereturned is based on certain conditions. For example:
if( x<= 0 )
return(0);
else
return (1);
⮚ FUNCTION CALLS
A function can be called by simply using the function name followed by a list of
actual parameters (or arguments), if any, enclosed in parentheses.
● When the compiler encounters a function call, the control is transferred to the
functionmul().
● This function is then executed line by line and a value is returned whena return
statement is encountered. This value is assigned to y.
● The function call sends two integer values 10 and 5 to the function.
● int mul(int x, int y)which are assigned to x and y respectively. The function
computes theproduct x and y, assigns the result to the local variable p, and then
returns the value 25 to the main where it is assigned to y again.
Page 4
Programming in C User defined functions
FUNCTION DECLARATION
Like variables, all functions in a C program must be declared, before they are
invoked. A function declaration (also known as function prototype) consists of four parts.
• Function type (return type).
• Function name.
• Parameter list.
• Terminating semicolon.
General format:
This is very similar to the function header line except the terminating semicolon.
When a function does not take any parameters and does not return any value, its
prototype is written as:
Points to note:
Page 5
Programming in C User defined functions
CATEGORY OF FUNCTIONS
A function, depending on whether arguments are present or not and whether a value is
returned or not, may belong to one of the following categories:
#include<stdio.h>
void printline(void);
main()
{
printline();
}
void printline(void)
{
int i;
for(i=0;i<=30;i++)
printf(“%d”,i);
printf(“\n”);
}
● When there is nothing to be returned, the return statement is optional.
● The closing brace of the function signals the end of execution of the function, thus
returning the control, back to the calling function.
● In this approach the calling function pass argument to the called function.
● The nature of data communication between the calling function and the called
function with arguments but no return value.
Page 6
Programming in C User defined functions
Example:
#include<stdio.h>
void printline(char c);
main()
{
printline(‘*’);
}
void printline(char c)
{
int i;
for(i=0;i<10;i++)
printf(“%c”,c);
printf(“\n”);
}
Example Program:
#include<stdio.h>
intvalue(int x, int y);
main()
{
int a=10,b=15,sum;
sum=value(a,b);
printf(“\n%d”,sum);
}
int value(intx,int y)
{
return(x+y);
}
● In this approach functions may not take any arguments. Hence no arguments are
passed.
● But it returns a value to the calling function.
Example:
int get_number(void);
main()
{
int m = get_number( );
printf("%d",m);
}
int get_number(void)
Page 7
Programming in C User defined functions
{
int number;
scanf("%d",&number);
return(number);
}
In pass by value, values of actual parameters are copied to the variables in the parameter
list of the called function. The called function works on the copy and not on the original
values of the actual parameters. This ensures that the original data in the calling function
cannot be changed accidentally.
Inpass by pointers (also known as pass by address), the memory addresses of the
variables rather than the copies of values are sent to the called function. In this case, the
called function directly works on the data in the calling function and the changed values
are available in the calling function for its use.
#include<stdio.h> #include<stdio.h>
int mul(int x,int y); int mul(int *x,int *y);
main() main()
{ {
int y,a=10,b=5; int y,a=10,b=5;
y=mul(a,b ); /* pass by value */ y=mul(&a,&b); /* pass by reference */
printf(“Product=%d”,y); printf(“Product=%d”,y);
}
} int mul(int *x,int*y)
int mul(int x,int y) {
{ int m= *x , n= *y;
return(x*y); return(m*n);
} }
● Variables declared inside a block or function are said to belong only to that
block and all referred as local variable .
● Value of local variables are available only in that block and outside it.
● Variables declared in the declaration part before the main function block are
called global Variables.
Page 8
Programming in C User defined functions
● The value of global Variable thought the program & in every block of the
program.
Example:
#include<stdio.h>
int x;
main()
{
int a;
}
Here a is a local variable. x is global variable.
}
int mul(int x,int y) /* function definition – FORMAL PARAMTER*/
{
return(x*y);
}
*****************
Page 9