Functions

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

3.

14 Types of Functions
i. Library Functions/Pre-Defined/ Built-in Functions
which perform some standard and
VC Library ofCCompiler has a collection of various functions
defined tasks.
as Library functions/Pre-Defined/Bui
V These functions written by designers of C Compilers are called
functions.
Ex:sqrt(n)- computes square root ofn.
pow(x,y)- computes x.
printf)- used to print the data on the screen.
scanf()- used to read the data from the keyboard.
abs(x)- computes absolute value of x.

i. User-Defined/ Programmer Defined Functions


Program
The functions written by the programmer/user to do the specific tasks is called User-Defined/
Defined Functions.
main() is the user defined function.

3.15 Elements of User-Defined Functions


V The three elements of user-defined functions are shown below:
i. Function Prototype/Declaration
ii. Function Definition
ii. Function Call
3.15.1 Function Declaration/Function Prototype
Before using a function, the compiler must know the number of parameters and the type of
parameters that the function expects to receive and the data type of value that it willreturn to the
calling program.
Placing the function declaration statement prior to its use enables the compiler to make a check on
the arguments used while calling that function.
The general format for declaring a function that accepts arguments and returns a value as result can be
given as:

return data type function name(data_type variable1, data _type variable2,..);


Here, function nameis a valid name for the function. A function should have a meaningful name that
must specify the task that the function will perform.
return data type specifies the data type of the value that will be returned to the calling function as a
result of the processing performed by the called function.
(data type variablel, data_type variable2, ...) is a list of variables of specified data types. These
variables are passed from the calling function to the called function. They are also known as arguments
or parameters that the called function accepts to perform its task.
Ex: int add(int a, int b);

Things to remember about function declaration:


After the declaration of every function, there should be a semicolon. If the semicolon is missing,
the compiler will generate an error message.
The function declaration is global.
Use of argument name in the function declaration is optional.
int func(int, char, floa t);
or

int func(int num, char ch, float fnum);


A function cannot be declared within the body of another function.
A function having void as its return type cannot return any value.
A function having void as its parameter list cannot accept any value. So the function declared as
void print();
does not accept any input/arguments fromthe calling function.
If the function declaration does not specify any return type, then by default, the function returns
an integer value. Therefore, when a function is declared as
sum(int a, int b);
Then the function sum accepts two integer values from the calling function and in sum returns an
integer value to the caller.
Some compilers make it compulsory to declare the function before its usage while other compilers
make it optional.

3.15.2 Function Definition


VWhen a function is defined, space is allocated for that function in the memory.
wA function definition comprises of two parts:
Function header
Function body
3.16 return STATEMENT
V The return statement terminates the execution of the called function and returns control to the
calling function.
When the return statement is encountered, the program execution resumes in the calling function a
the point immediately following the function call.
V A return statement may or may not return a value to the calling function.
V The syntax of return state can be given as
return <expression>;
Here expression is placed in between angular brackets because specifying an expression is
optional.
VA function that has void return type cannot return any value to the calling function.

3.17 Passing Parameters to Functions


V There are two ways in which arguments or parameters can be passed to the called function.
Call by value: The values of the variables are passed by the calling function to the called function.
Call by reference The addresses of the variables are passed by the calling function to the calle
function.

1. Call by Value
V In this method, the called function creates new variables to store the value of the argument:
passed to it. Therefore, the called function uses a copy of the actual arguments to perform it
intended task.
V If the called function is supposed to modify the value of the parameters passed to it, then th
change will be reflected only in the called function. In the calling function, no change willb
made to the value of the variables. This is because all the changes are made to the copy of th
variables and not to the actual variables.

Example: Write a Cprogram to add two numbers using call by value.


#include<stdio.h>
int add (int a, int b)

int sum;
sum =a + b:
Output:
return sum: Enter the values of a and b: 4 5
result =9

void main()

int a,b, res;


printf"Enter the values of a and b:");
scanf(%d%d",&a,&b);
res = add(a,b);
printf(*result -%dn", res);
V Following are the points to remember while passing arguments to a function using the call-by value
method:
When arguments are passed by value, the called function creates new variables of the same
data type as the arguments passed to it.
The values of the arguments passed by the calling function are copied into the newly created
variables.
Values of the variables in the calling functions remain unaffected when the arguments are
passed using the call-by-value technique.
Pros and cons
The biggest advantage of using the call-by-value technique is that arguments can be passed as
variables, literals, or expressions, while its main drawback is that copying data consumes
additional storage space.
V In addition, it can take a lot of time to copy, thereby resulting in performance penalty, especially if
the function is called many times.

2. Call by Reference
V When the calling function passes arguments to the called function using the call-by-value
method, the only way to return the modified value of the argument to the caller is explicitly
using the return statement. A better option is to pass arguments using the call-by-reference
technique.
V In this method, we declare the function parameters as references rather than normal variables.
When this is done, any changes made by the function to the arguments it received are also
visible in the calling function.
V To indicate that an argument is passed using call by reference, an asterisk (*) is placed after the type
in the parameter list. Hence, in the call-by-reference method, a function receives an implicit
reference to the argument, rather than a copy of its value. Therefore, the function can modify the
value of the variable and that change will be reflected in the calling function as well.

Example:
1. Write a C program to add two numbers using call by reference.
#include<stdio.h>
int add (int *a,int *b)
int sum;
sum = *a + *b:
return sum;

void main()
{
int a,b, res; Output:
printf("Enter the values of a and b:"); Enter the values of aand b: 45
result =9
scanf("%d%d",&a, &b);
res = add(&a, &b);
printf("result -%dn", res);
2. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b-temp;
void main()

int a,b;
printf(Enter the values of a and b:"); Enter the values of aand b: 10 20
scanf("%d%d",&a,&b); Before swapping: a-10 b-20
printf("Be fore swapping: a-%d\tb-%d", a, b); After swapping: aF20 b=10
swap(&a,&b);
printf(After swapping: a-%ditb=%d", a, b)

Output:
Enter the values of aand b: 10 20
Before swapping: a=10 b=20
After swapping: a=20 b=10

Advantages
V Since arguments are not copied into the new variables, it provides greater time and space efficiency.
The function can change the value of the argument and the change is reflected in the calling
function.
A function can return only one value. In case we need to return multiple values, we can pass
those
arguments by reference, so that the modified values are visible in the calling function.

Disadvantages
However, the drawback of using this technique is that if inadvertent changes are caused to variables
in called function then these changes would be reflected in calling function as
would have been overwritten.
original values

You might also like