c Programming-chapter 08
c Programming-chapter 08
PRESENTED BY
SHEYONA G
CHAPTER-08
USER DEFINED FUNCTIONS
Function
Function is a set of instructions to carry out a particular task.
or
A function is a self – contained block of code that performs a particular task.
‘C’ functions are classified into two categories. They are
⮚ Library/ Built-in / standard functions
Ex: printf(), scanf(), strlen(), etc.
⮚ User-defined functions.
Ex: main()
Characteristics of user defined functions:
⮚ The function main( ) is specially recognized in ‘C’. Every program must have a main function
to indicate where the program has to begin its execution.
⮚ It is not possible to code any program only with main function because it leads to a number
of problems. The program may become too large and complex and as a result the task of
debugging, testing, and maintaining becomes difficult.
⮚ If a program is divided in to independently coded part and later combined into a single unit.
⮚ The independently coded programs are called ‘subprograms’ that are easy to understand,
debug, and test. In C, such programs are referred to as ‘functions’.
Elements of user defined function
⮚ Return Type
⮚ Function Name
⮚ Parameter list
⮚ Local variable declaration
⮚ Executable Statements
⮚ Return statement
All the six elements are grouped into two parts,
executable statement2;
.......
return statement;
}
Function Header
⮚ It consists of three parts, return type, function name and parameter list.
⮚ The return type specifies the type of the value that the function is returning to the calling program.
⮚ If return type is not specified, by default C will assume it as an integer type.
⮚ If the function is not retuning anything, then user needs to specify the return type as void.
⮚ The parameter list declares the variables that will receive the data sent by the calling program.
⮚ They serve as input data to the function to carry out the specified task.
⮚ They are often referred to as formal parameters.
⮚ The parameter list contains declaration of variables separated by commas and surrounded by parentheses.
Function Body
The function body contains declaration and executable statements necessary for performing
the required task. The function body enclosed with in curly braces, contains three parts are
⮚ Local declaration that specify the variables needed by the function.
⮚ Executable statements that performs the task to the function.
⮚ A return statement that returns the value evaluated by the function.
Return values and their types
A function may or may not send back (return) any values to the calling function. If it returns, it is
done by “return” statement. The called function can only return one values per call, at the most.
The return statement can take one of the following forms:
1. Simple return statement : return;
2. Return with expression : return (expression);
Ex: return a;
return -1;
return(5);
return(a + b* c);
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.
Ex:
main()
{
int y;
y=mul(10,12);
printf(“%d”,y);
}
Category of functions
Depending on the arguments and return type of functions, they are categorized into
following types.
⮚ Function with no arguments and no return type.
⮚ Function with arguments but no return type.
⮚ Function with no arguments but return type.
⮚ Function with arguments and return type.
Function with no arguments and no return type
When a function has no arguments and no return type, it does not receive any data
from the calling function and it does not return any value to the calling function
Ex:
void sum();
main()
{
sum();
}
void sum()
{
int a,b,s;
printf(“enter two number”);
scanf(“%d%d”,&a,&b);
s=a+b;
printf(“sum=%d”,s);
}
Function with arguments and no return type
When a function has arguments and no return type, the called function receives data
from the calling function and it does not return any value to the calling function
void sum(int a, int b );
void main()
{
int a,b;
printf(“Enter two numbers\n”);
scanf(“%d%d”,&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
int s;
s=a+b;
printf(“Sum is= %d”, s);
}
Function with no arguments but return type
When a function has no arguments but a return value, the called function does not receive any
data from the calling function but after executing the function body it returns the result to the
calling function.
int sum();
main( )
{
int s;
s=sum( );
printf(“Sum is %d”,s);
}
int sum( )
{
int a,b,s;
printf(“Enter 2 numbers\n”);
scanf(“%d%d”,&a,&b);
s=a+b;
return(s);
}
Function with arguments and return type
When a function has both arguments and return value, the called function receives data from
the calling function and after executing the function body it returns the result to the calling
function.
int sum(int a,int b);
main( )
{
int s,a,b;
printf(“Enter 2 numbers\n”);
scanf(“%d%d”,&a,&b);
s=sum(a,b);
printf(“Sum is %d”,s);
}
int sum(int a, int b)
{
int s;
s=a+b;
return(s);
}
Nesting of functions
A function will call another function is called nesting of function.
For example : main() can call function1, which can call function2, function2 can call function2 and so on
void fun1()
#include<stdio.h>
{
#include<conio.h>
int a,b;
void fun1();
printf(“Enter two numbers\n”);
void fun2(int,int);
scanf(“%d%d”,&a,&b);
void main()
fun2(a,b);
{
}
clrscr();
void fun2(int x,int y)
fun1();
{
getch();
int c;
}
c=x+y;
printf(“Sum of two numbers=%d”,c);
}
Recursion
when a function calls itself again and again is called as recursion.
Ex:
main() output :
{
printf(“Example for recursion\n”); Example for recursion
main(); Example for recursion
} Example for recursion
Function with arrays
Passing 1-D arrays to Function
To pass an one-dimensional array to a function, it is necessary to give the name of the array, without any
subscript, and the size of the array as arguments.
Ex: average (a, n);
will pass the whole array a to the called function.
Which of the following is a valid return statement? return(20); return(2,3); return(a,b); RETURN(A*B):
By default ________ is the return type of a C function. char float int double
The parameter defined in function definition is called ________. formal parameter actual parameter static variable register
variable
When we place the declaration above all the functions, the Local prototype Global prototype Both a& b function
prototype is referred as header
In the absence of return statement in the function, ______ act as void closing brace void return int
return.
In C language the return type is optional, when the function returns ________
char float int double
type data
Which amongst the keyword is used for returning a value from a function. double for break return
The ______ keyword is used to return a value from a function. return exit break void
When we place the function declaration inside the main() functions, the
Local prototype Global prototype Both a& b function header
prototype is referred as ________.
Which of the following character is used at the end of a function prototype? comma colon semicolon dot
function
In which of the following function, calling function and called function are same. recursion library function return
prototype
function
A function which calls itself is called _____. Recursion library function return
prototype
Which of the function call is a valid statement ,If int a[10]; and "display" is the display(a[10]
display(a); display(a[10]); all the above
function name. [10]),
In passing array to functions, function call must have the name of the array to be
square brackets comma colon semicolon
passed without ______.
The program execution always begin with ____ function main() clrscr() getch() closing brace
If a function does not return any value, the return type must be declared void int float char
as _________.