Unit5 - Functions

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

1

UNIT - 5

Functions ( Method Or A Sub-Routine Or A Procedure)


A function is a group of statements that work together to perform a specific task.
Functions are classified into 2.
a) Built-in functions – Functions that are pre-defined in Library files. They can be
used in our programs, when we have to perform certain operations. Header(Library)
files in which they are defined, should also be included in our programs.
Eg: ‘strcat(str1,str2)’ is a string-handling function defined in ‘string.h’.
b) User-defined function – Functions that are defined by users.
Every C program has at least one function, which is main().

User-defined functions - A function has 3 parts:


1) Function Declarations:
A function declaration tells the compiler about a function name and how to call the function.
The actual body of the function can be defined separately.
General syntax: return_type function_name( parameter list );

Eg: int max(int num1, int num2);

Parameter (Argument) names are not important in function declaration only their type is
required, so following is also valid declaration: int max(int, int);

2) Calling a Function:
While creating a C function, we must provide a definition of, what the function has to do. To
use a function, we have to call that function to perform the defined task.

When a program calls a function, program control is transferred to the called function. A
called function performs defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns program control back to the main
program.

To call a function, we simply need to pass the required parameters along with function name,
and if function returns a value, then we can store returned value.

General syntax : function_name(parameter list);

3) Defining a Function:
The general form of a function definition in C programming language is as follows:

return_type function_name( parameter list )


{
body of the function
}
2

A function definition in C programming language consists of a function header and


a function body. Here are all the parts of a function:
 Return Type: A function may return a value. The return_type is the data type of the value
the function returns. Some functions perform the desired operations without returning a
value. In this case, the return_type is the keyword void.
 Function Name: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
 Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value
to the parameter. This value is referred to as actual parameter or argument. The parameter list
refers to the type, order, and number of the parameters of a function. Parameters are optional;
that is, a function may contain no parameters.
 Function Body: The function body contains a collection of statements that define what the
function does.
For example:

int max(int num1, int num2); /* function declaration */


void main ()
{ int a = 100; /* local variable definition */
int b = 200;
int ret;
ret = max(a, b); /* calling a function to get max value */
printf( "Max value is : %d\n", ret );
}
int max(int num1, int num2) /* function returning the max between two numbers */
{ int result; /* local variable declaration */
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Max value is : 200

Function Arguments:
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function. The formal
parameters behave like other local variables inside the function and are created upon entry
into the function and destroyed upon exit.

Types of Functions, Call by Value & Call by Reference


Type of User-defined Functions in C
There can be 4 different types of user-defined functions, they are:

 Function with no arguments and no return value


 Function with no arguments, but return value
 Function with arguments and no return value
 Function with arguments and return value
3

1) Function with no arguments and no return value

In this method, no arguments are passed to the called function (Addition() )& no values are
returned to the calling function (main().

2) Function with no arguments, but return value

In this method, no arguments are passed to the called function (Multiplication()), but a value is
returned to the calling function (main()). The data type of the return value will depend upon the
return type of function declaration. For instance, if the return type is int, then return value will be
int.
4

3) Function with arguments and no return value

In this method , arguments are passed to the called function (Addition()), but does not return any
value to the calling function (main()).

4) Function with arguments and return value

This method allows to pass the arguments to the called function (Multiplication()) & returns some
value to the calling function (main()). Data type of the return value will depend upon the return type
of function declaration. For instance, if the return type is int, then return value will be int.
5

Call by Value & Call by Reference


I. Call by value method copies the value of an argument into the formal parameter of
that function. Therefore, changes made to the parameter of the main function do
not affect the argument. In this parameter passing method, values of actual
parameters are copied to function's formal parameters, and the parameters are
stored in different memory locations. So any changes made inside functions are not
reflected in actual parameters of the calling function.

void swap(int , int); //prototype of the function


int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and
b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters
do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b
= 10
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

II. Call by reference method copies the address of an argument into the formal
parameter. In this method, the address is used to access the actual argument used in
the function call. It means that changes made in the parameter alter the passing
argument. In this method, the memory allocation is the same as the actual
parameters. All the operation in the function are performed on the value stored at
the address of the actual parameter, and the modified value will be stored at the
same address.

void swap(int *, int *); //prototype of the function


int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and
b in main
swap(&a,&b);
6

printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters
do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20
, b = 10
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

Parameters Call by value Call by reference


While calling a function, when you pass While calling a function, in programming language instead of
Definition values by copying variables, it is known as copying the values of variables, the address of the variables is
"Call By Values." used it is known as "Call By References.
In this method, a copy of the variable is
Arguments In this method, a variable itself is passed.
passed.
Changes made in a copy of variable never
Change in the variable also affects the value of the variable
Effect modify the value of variable outside the
outside the function.
function.
Alteration of Does not allow you to make any changes in Allows you to make changes in the values of variables by using
value the actual variables. function calls.
Passing of Values of variables are passed using a
Pointer variables are required to store the address of variables.
variable straightforward method.
Value
Original value not modified. The original value is modified.
modification
Memory Actual and formal arguments will be Actual and formal arguments will be created in the same
Location created in different memory location memory location
Actual arguments are not Safe. They can be accidentally
Actual arguments remain safe as they
Safety modified, so you need to handle arguments operations
cannot be modified accidentally.
carefully.
Default in many programming languages It is supported by most programming languages like JAVA, but
Default
like C++.PHP. Visual Basic NET, and C#. not as default.

Function as an argument

A function can be passed as an argument of another function.


int double(int m);
int square(int k);
void main()
7

{
int y=2,x;
x = double(square(y)); printf(“x=%d”,x);
}
int double(int m)
{
int p;
p=m*2;
return(p);
}
int square(int k)
{
int z;
z = k * k;
return(z);
}
Output: x = 8

Nesting of Functions (Nested Functions)


Nested function (or nested procedure or subroutine) is a function which is defined within
another function,
int main()
{
add();
}
int add()
{
mul();
int a,b,c;
printf("Enter two numbers for addition :\n");
scanf("%d %d",&a,&b);
c=a+b;
printf("Sum = %d\n",c);
}
int mul()
{
int x,y,z;
printf("Enter two numbers for multiplication :\n");
scanf("%d %d",&x,&y);
z=x*y; printf("Product = %d\n\n",z);
}
Output:
Enter two numbers for multiplication : 6 5
Product = 30
Enter two numbers for addition : 20 15
Sum = 35

***************************

You might also like