0% found this document useful (0 votes)
2 views

c Programming-chapter 08

The document provides an overview of user-defined functions in C programming, detailing their characteristics, elements, and types. It explains the importance of functions in modular programming, the structure of function declarations, definitions, and calls, as well as different categories based on arguments and return types. Additionally, it includes examples and assignments related to function usage and concepts such as recursion and array passing.

Uploaded by

flxv07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Programming-chapter 08

The document provides an overview of user-defined functions in C programming, detailing their characteristics, elements, and types. It explains the importance of functions in modular programming, the structure of function declarations, definitions, and calls, as well as different categories based on arguments and return types. Additionally, it includes examples and assignments related to function usage and concepts such as recursion and array passing.

Uploaded by

flxv07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

WELCOME

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:

⮚ A function is a self - contained program segment.


⮚ It carries out some specific, well-defined task.
⮚ A function in C supports the concept of modular programming design techniques.
⮚ It breaks large computing tasks into smaller ones.
⮚ A function can return a value to the calling program.
Need for 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

The three elements related to user defined functions are,


1) Function Declaration
2) Function Call
3) Function Definition
Function declaration or Function prototype

❖ All function in a C program must be declared, before they invoked.


❖ It gives the compiler the details about the function such as the number and types of
arguments and the type of return values.
❖ A function declaration is also known as function prototype.
It consists of four parts.
⮚Return type
⮚Function name
⮚Parameter list
⮚Terminating semicolons.
They are coded in the following format.
return_type function_name (parameter list);

Ex: int mul(int, int);


int mul( int a, int b);
⮚ The parameter list must be separated by commas.
⮚ The parameter names do not need to be same in the prototype declaration and the function definition.
⮚ Use of parameter names in the declaration is optional.
⮚ If the function has no formal parameters, the list is written as void.
⮚ It is also possible to pass the value of an array to a function. The return type is optional, when the
function returns int type of data.
⮚ The return must be void if no value is returned.
⮚ When the declared types do not match with the types in the function definition, compiler will produce
error.
Function Definition
A function definition or function implementation shall include the following elements.

⮚ Return Type
⮚ Function Name
⮚ Parameter list
⮚ Local variable declaration
⮚ Executable Statements
⮚ Return statement
All the six elements are grouped into two parts,

⮚ Function Header ( First Three elements)


⮚ Function Body ( Second Three elements)
A general format of a function definition to implement these two parts:
return_type function_name (parameter list)
{
local variable declaration;
executable statement1;

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.

Function 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 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.

⮚ The function prototype will be as follows.


float average (float [], int);
⮚ The function average header will be
float average( float arr[], int size);
⮚ The function average is defined to take two arguments, the array name and the size of the array.
⮚ No need to specify the size of the array in the function header.
float average(float [], int); float average(float a[], int size)
void main() {
{ float i,avg=0;
float a[5],i,n=5; for( i=0;i<size;i++)
printf(“Enter array values\n”); {
for(i=0;i<n;i++) avg=avg+a[i];
scanf(“%f”,&a[i]); }
average(a,n); printf(“Average=%f”,avg);
getch(); }
}
Passing 2-D arrays to Function
It is also possible to pass multi-dimensional arrays to the function. The rule to pass 2D arrays is as follows.

⮚ The function must be called by passing only the array name.


For Ex: sum( a,n);
⮚ In the function definition, we must indicate that the array has
2D by including two set of array brackets.
⮚ The size of the second dimension must be specified.
void sum ( int a[][3], int size);
⮚ The prototype declaration should be similar to the function header.
void sum (int [][3], int );
void sum(int [3][3], int); }
void main() printf(“\n”);
{ }
sum(a,3);
int a[3][3],i,j;
getch();
printf(“Enter matrix values\n”); }
for(i=0;i<3;i++) void sum(int a[][3], int n)
{ {
for(j=0;j<3;j++) int s=0,i,j;
{ for(i=0,i<n;i++)
scanf(“%d”,&a[i][j]); {
} for(j=0;j<n;j++)
} {
s=s+a[i][j];
for(i=0;i<3;i++)
}
{ }
for(j=0;j<3;j++) printf(“Sum=%d”, s);
{ }
printf(“%d”,a[i][j]);
ASSIGNMENTS:
1) What are the 4 parts of function declaration?
2) What do you mean by global and local function prototype?
3) What are the 2 main parts of function definition?
4) Briefly explain the three elements of user defined functions with example.
5) Write any 2 categories of function in C language.
6) Write a C program to find sum of two integer numbers using the function with return type and with
arguments.
7) Write a C program to find sum of two integer numbers using the function with return type and no
arguments.
8) Write a C program to find sum of two integer numbers using the function with no return type and with
arguments.
9) Write a C program to find sum of two integer numbers using the function with no return type and no
arguments.
10)Briefly explain the category of functions with example.
formal parameter actual parameter static variable register
The parameter used in function call is called ________.
variable
Independently coded programs in C language is called as _____. sub programs union structure pointer

printf () function is an example for_______________ user defined library recursion programmer


defined
void main() is an example for ___________. user defined library recursion programmer
defined
A function may be called __________from any other functions. only once only twice more than only three
once times
In a function definition, the parameter list must be separated by semicolon colon comma dot
__________.
In function prototype specifying ______ is optional. variable name function name return type method name

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 _________.

You might also like