1BCA 4 Unit-6 Function - 06

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

Semester- 1st

Paper Code:- 1BCA 4


Unit – 6 ( functions )

Introduction:-

A function is a self-contained block of statements that perform a coherent task of


some kind. Every C program can be thought of as a collection of these functions
and at least has a one function, which is main().

We can divide up our code into separate functions. How we divide up our code
among different functions is up to us, but logically the division is such that each
function performs a specific task.
A function declaration tells the compiler about a function's name, return type,
and parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that our program can
call. For example, strcat() to concatenate two strings, memcpy() to copy one
memory location to another location, and many more functions.
A function can also be referred as a method or a sub-routine or a procedure, etc.

Defining a Function:-

The general form of a function definition in C is as follows :-


return_type function_name ( parameter list )
{
body of the function
}
A function definition in C programming 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.

R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 1


 Parameters :- A parameter is like a placeholder. When a function is invoked,
we 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:-

Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}

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.
A function declaration has the following parts:-
return_type function_name( parameter list );
For the above defined function max(), the function declaration is as follows:-
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so
the following is also a valid declaration:-
int max(int, int);
Function declaration is required when we define a function in one source file and we call
that function in another file. In such case, we should declare the function at the top of the
file calling the function.

R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 2


Calling a Function:-

While creating a C function, we give a definition of what the function has to do. To use
a function, we will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.
To call a function, we simply need to pass the required parameters along with the
function name, and if the function returns a value, then we can store the returned value.
For example :-
#include <stdio.h>

/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;

/* calling a function to get max value */


ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
We have kept max() along with main() and compiled the source code. While running the final
executable, it would produce the following result:-
Max value is : 200

R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 3


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.
Formal parameters behave like other local variables inside the function and are created
upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a
function:-
1. Call by value (sending the values of the arguments) :-

Whenever we called a function and passed something to it we have always passed


the ‘values’ of variables to the called function. Such function calls are called
‘calls by value’.
This method copies the actual value of an argument into the formal parameter of
the function. In this case, changes made to the parameter inside the function have
no effect on the argument.
In this method, the ‘value’ of each of the actual arguments in the calling function
is copied into corresponding formal arguments of the called function.
For Example:-
#include <stdio.h>
void swap( int x, int y );
void main()
{
int a=10, b=20;
swap(a, b); // Function call by value
printf(“a=%d b=%d\n”, a,b);
getch();
}
void swap( int x, int y )
{
int n;
n=x;
x=y;
y=n;
printf(“x = %d y = %d\n”, x,y);
}
Output:-
x = 20 y =10
a = 10 b = 20

R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 4


2. Call by reference (sending the addresses of the arguments):-

Instead of passing the value of a variable, we can pass the location number (also
called address) of the variable to a function, this method is called ‘call by
reference’.
In this method, the addresses of an actual arguments in the calling function are
copied into the formal arguments of the called function. Inside the function, the
address is used to access the actual argument used in the call. This means that
changes made to the parameter affect the argument.
For Example:-
#include <stdio.h>
void swap( int *, int * );
void main()
{

int a=10, b=20;


swap(&a, &b); // Function call by reference
printf(“a=%d b=%d\n”, a,b);
getch();
}
void swap( int *x, int *y )
{
int n;
n = *x;
*x = *y;
*y = n;
}
Output:-
a = 20 b = 10
Note:- By default, C uses call by value to pass arguments. In general, it means the code within
a function cannot alter the arguments used to call the function.

Library Function:-
As you know, there are two types of functions in C. They are: library functions and
user defined functions.
Library functions are inbuilt functions which are available in common place called
C library. Where as, User defined functions are the functions which are written by
us for our own requirement.

R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 5


Adding user defined function to the Library:-

Normal header files are saved as “file_name.h” in which all library functions are
available. These header files contain source code and this source code is added in
main C program file where we add this header file using “#include <file_name.h>”
command.
We can add user defined function to the library. It makes sense in doing so as the
functions that are to be added to the library are first compiled and then added. When
we use these functions (by calling them), we save on their compilation time as they
are available in the library in the compiled form.
Let us now see how to add user defined functions to the library. Different compilers
provide different utilities to add/delete/modify functions in the standard library. For
example C compilers provide a utility called ‘tlib.exe’ (Turbo Librarian). Let us use
this utility to add a function factorial() to the library.
Given below are the steps to do so:-
a) Write the function definition of factorial( ), say ‘fact.c’

int factorial ( int num )

{
int i, f = 1;
for ( i = 1; I <= num; i++ )
f = f * i;
return ( f );
}
b) Compile the ‘fact.c’ file using Alt F9. A new file called ‘fact.obj’ will get
created.
c) Add the function to the library ‘maths.lib’ by issuing the command
c:\>tlib maths.lib + c:\fact.obj
d) Declare the prototype of the factorial( ) function in the header file, say
‘fact.h’. This file should be included while calling the function.
e) To use the function present inside the library, we will create a program as:-
#include “c:\fact.h”
#include <stdio.h>
int main( )
{
int f;
f = factorial ( 5 );
printf( “%d”\n”, f);
return 0;
}
R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 6
f) Compile and execute the program using Ctrl F9.

R.B.Singh, HOD, Deptt. of BCA, A.S.College, Bikramganj Page 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy