Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Functions

Function
The function is a self-controlled program that carries out some specific,
Well defined task. The large problem may be decomposed into small or
easily manageable parts or modules called function.

C function can be classified into two categories


Library function

A library function is also called system defined function or inbuild


function. The printf() & scanf() functions are the example of library
function. All the library functions are stored in the standard c library,
which is accessed through the header file. To use these library functions,
the user needs to include a header file.

User-defined function

The programmer develops the user-defined functions at the time of


writing the program i.e the user-defined function is created by the user at
the time of writing the program and this function is called the user-
defined function

Need of user-defined function


The reasons for the need of user-defined functions are
1. Using user-defined functions the complex programs become easier to
read, write, debug and test. If the complex program is divided into
functional parts, each part may be independently coded and later
combined into a single unit. This subprogram is called functions that are
much easier to design and understand.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


2. Writing the user-defined function avoid rewriting the same code again
and again. This saves both time and space.

Advantages of user-defined function


1. Easy to write a correct small function.
2. Easy to read, write and debug a function
3. Easier to maintain or modify such function.
4. It can be called any number of times in any place with different
arguments.
5. Ovoid rewriting the same code again and again.
6. Save both time and space.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


Elements of User-Defined Functions
Functions are classified as one of the derived data types in C. The
functions c are defined and used like any other variables in C programs.
There exist some similarities between functions and variables in C.
 Both function names and variable names are considered identifiers
and therefore they must stick to the rules for identifiers.
 Like variables, functions have types (such as int) associated with
them.
 Like variables, function names and their types must be declared
and defined before they are used in a program.
In order to make use of a user-defined function, we need to establish
three elements that are related to functions.
1. Function Definition: The function definition is an independent
program module that is specially written to implement the
requirements of the function. In order to use this function, we need
to invoke it at a required place in the program. This is called
function.
2. Function Call: The program or a function that calls the function is
referred to as the calling program or calling function.
3. Function Declaration: The calling program should declare any
function (like a declaration of a variable) that is to be used later in
the program. This is known as the function declaration or function
prototype.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


Defining the function
The general format of defining the function is
return-type function-name(argument list with declaration)
{
function body;
……………………
……………………
……………………
return(expression);
}

The format of function definition consists of six parts


1. Return type of function
2. Function-name
3. Formal argument...
4. The opening curly brace {
5. Function body
6. The closing curly brace}

1. Return type of function


The functions return types represent the data type of the item that is
returned by the function. The return type may be basic data type or
secondary data type. If you want the function not to return any value
then it may be declared as a void type. The void data type informs the

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


compiler not to save any temporary space for a value to send back to the
calling program. For example

int function-name(…….)
float function-name(………)
void function-name(……..)
char function-name(…….)

2. Function name
The function name is an identifier. The naming of a function must
follow the same rules as an identifier. Whenever the function is called
the control is transferred to the function name. The function name is
relevant to the function operation. For example
sum()
square()
sub()

3. Argument
The argument is valid variable names separate by a comma and they
must be declared within the parentheses. The semicolon is not allowed
after closing parenthesis. The function receives the data into the
variables from the calling function, these variables are called a formal
argument. The formal arguments are also known as parameters or formal
parameters.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


The general formal of declaring a formal argument is
return-type function-name(data type argl, data type arg2, data type argn)
{
…………..
…………
}
Where arg1, arg2,--argn is the formal arguments. Data type may be basic
data type or secondary data type.
For example
sub(int a, int b) //a, b are called formal argument
{
………….
…………..
}

4. The opening and closing curly braces


The opening curly brace (is the indication of the beginning of the
function body. The closing curly brace) is the indication of the end of
the function body. If these braces are not present then the compiler
generates an error.
5. Function body
The statements between open curly brace { and close curly brace) is the
function body. The function body is a place that contains local variable
declarations and executable statements. The return keyword is used to
terminate the function and return the value to the calling function. The

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


return statement may also be used to exit a function without returning a
value. The return statement may or may not include in the function
body. The return value must be corresponding with the return type.

Meaning of function definition


Example 1
int add(int a,int b)
{
int c;
c=a+b:
return(c);
}
It is an instruction given by the user to the compiler, it tells to the
compiler that, define a function having a particular name, having a
particular argument, having a particular datatype.
1) It is an instruction given by the user to the compiler, it tells to the
compiler that, defines a function having name add, having
argument int a, int b, having data type int.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


Declaration of function/function prototype
Like variables, all the functions in a c must be declared, before they are
invoked.
The general format of declaration of the function is
Return-type function-name(argument list with declaration);
where we declare a function
Above all the function (including main)

A function declaration consists of the following part.


1. Return type of function
2. Function-name
3. Formal argument
4. Semicolon;
Meaning of declaration of function
Example 1:
int add(int a, int b);
1) It is an instruction given by the user to the compiler, it tells to the
compiler that, declare a function having name add, having
argument int a, int b, having data type int.
Example 2:
float sub (float a, float b);
2) It is an instruction given by the user to the compiler, it tells to the
compiler that, declare a function having name sub, having
argument float a float b, having datatype float.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


Function calls/Accessing a function
A function can be called or accessed by simply the function name,
followed by a list of arguments enclosed in the parentheses.
Syntax
function-name(argument list);
where we called a function?
Inside the main () between the clrscr()b and getch().
Meaning of function calls
1. Swap(x);
2. add(a,b);
It is an instruction given by the user to the compiler, it tells to the
compiler that, execute the function declaration.
1. It is an instruction given by the user to the compiler, it tells to the
compiler that, execute the swap function.
2. It is an instruction given by the user to the compiler, it tells to the
compiler that, execute the add function.

Actual argument: The Arguments which sent the information to the


formal argument.
Formal argument: The Arguments which received the information to
the actual argument.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


int add (int a, int b); //function declaration
void main()
{
clrscr();
add(actual argument); //function call
getch();
}
int add (int a, int b) //function definition
{ formal argument
…………….
…………….
…………….
…………….
}

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


RETURN VALUES AND THEIR TYPES
A function may or may not send back any value to the calling function.
If it does, it is done through the return statement. While it is possible to
pass to the called function any number of values, the called function can
only return one value per call, at the most.
The return statement can take one of the following forms:

return;
or
return (expression);

The first, the 'plain' return does not return any value; it acts much as the
closing brace of the function. When a return is encountered, the control
is immediately passed back to the calling function.

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


//Program for use of function
#include<stdio.h>
#include<conio.h>
void pqr(); // function declaration/prototype
void main()
{
clrscr();
printf("In main function,before function call.\n");
pqr(); //function call
printf("In main function,after function call.\n");
getch();
}
void pqr() // function definition
{
printf("Welcome to the function pqr\n");
}
/* Output
In main function,before function call.
Welcome to the function pqr
In main function,after function call.
*/

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


//Program for use of function
#include<stdio.h>
#include<conio.h>
void pqr(); // function declaration/prototype
void abc();
void main()
{
clrscr();
printf("In main function,before function call.\n");
pqr(); //function call
printf("In main function,after function call.\n");
abc(); //function call
getch();
}
void pqr() // function definition
{
printf("Welcome to the function pqr\n");
}
void abc() // function definition
{
printf("Welcome to the function abc\n");
}
/*Output
In main function,before function call.
Welcome to the function pqr
In main function,after function call.
Welcome to the function abc */

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


//Pragram for addition of two no using function
#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
void sum()
{
int a,b,sum=0;
printf("Enter two no=");
scanf("%d%d",&a,&b);
sum=a+b;
printf("sum of two no is=%d",sum);
}
/* Output
Enter two no=30 40
sum of two no is=70
*/

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806


//Pragram for multiplication of two no using function
#include<stdio.h>
#include<conio.h>
void mul();
void main()
{
clrscr();
mul();
mul();
getch();
}
void mul()
{
int a,b,mul;
printf("Enter two no=");
scanf("%d%d",&a,&b);
mul=a*b;
printf("multiplication of two no is=%d\n",mul);
}
/* OUTPUT
Enter two no=4 4
multiplication of two no is=16
Enter two no=5 5
multiplication of two no is=25 */

Prof.Rohan R. Bhale,HVPM College Amravati,7721988806

You might also like