Sie sind auf Seite 1von 76

Amity Institute of Geoinformatics & Remote Sensing

Name(AIGIRS)
of Institution

Module IV: Functions

M.Tech/M.Sc.(GIS&RS), 2nd Semester


Computer Fundamentals and C Programming (CSIT 634)

Dr. Deepak Kumar


AIGIRS
Name of Institution

Introduction to
C Functions
Introduction AIGIRS
Name of Institution

1. A function is a group of statements that together


perform a task.
2. Every C program has at least one function, which is
main(), and all the programs can define
additional functions.
3. We can divide up the code into separate functions/
basic building blocks called C function.
4. C function contains set of instructions enclosed by
{ } which performs specific operation.
5. In fact, Collection of these functions only creates a C
program.
Advantages of functions AIGIRS
Name of Institution

1. C functions are used to avoid rewriting same logic/code


again and again in a program.
2. There is no limit in calling C functions to make use of
same functionality wherever required.
3. We can call functions any number of times in a program
and from any place in a program.
4. A large C program can easily be tracked when it is divided
into functions.
5. The core concept of C functions are, re-usability, dividing
a big task into small pieces to achieve the functionality
and to improve understandability of very large C
programs.
Types of Functions AIGIRS
Name of Institution

There are two types of functions in C programming:


1. Standard library functions
2. User defined functions
AIGIRS
Name of Institution

Standard Library
Functions
Standard Library functions-Examples AIGIRS
Name of Institution

1. The C language is accompanied by a number of


standard library functions which carry out various useful tasks.

2. Functions are built-in functions in C to handle tasks such as


mathematical computations, I/O processing, string handling etc.

3. In particular, all input and output operations (e.g., writing to the


terminal) and all math operations (e.g., evaluation of sines and
cosines) are implemented by library functions.

4. These functions are defined in the header file. When you


include the header file, these functions are available for use.
Standard Library functions-Examples AIGIRS
Name of Institution

1. The printf() is a standard library function to


send formatted output to the screen (display
output on the screen). This function is defined
in "stdio.h" header file.
2. There are other numerous library functions
defined under "stdio.h", such as scanf(),
fprintf(), getchar() etc.
Standard Library functions-Examples AIGIRS
Name of Institution

1. printf() and scanf() functions are inbuilt library


functions in C programming language which are
available in C library by default. These functions are
declared and related macros are defined in stdio.h
which is a header file in C language.

2. We have to include stdio.h file as shown in below C


program to make use of these printf() and scanf()
library functions in C language.
printf() function in C language AIGIRS
Name of Institution

1. In C programming language, printf() function is used to print the


character, string, float, integer, octal and hexadecimal values
onto the output screen.

2. We use printf() function with %d format specifier to display the


value of an integer variable.

3. Similarly %c is used to display character, %f for float variable, %s


for string variable, %lf for double and %x for hexadecimal variable.

4. To generate a newline, we use \n in C printf() statement.

C language is case sensitive.


For example, printf() and scanf() are different from Printf() and Scanf(). All
characters in printf() and scanf() functions must be in lower case.
getch() function in C language AIGIRS
Name of Institution

getch() function is a function in C programming


language which waits for any character input from
keyboard.
getchar() and putchar() function AIGIRS
Name of Institution

1. putchar() function is a file


handling function in C
programming language
which is used to write a
character on standard
output/screen.
2. getchar() function is used
to get/read a character from
keyboard input. Please find
below the description and
syntax for above file
handling function.
getchar() and putchar() function example AIGIRS
Name of Institution
gets() function example AIGIRS
Name of Institution
getc() and putc() function AIGIRS
Name of Institution

getc(), putc() functions are file handling function


in C programming language which is used to read
a character from a file (getc) and display on
standard output or write into a file (putc).
Please find below the description and syntax for
above file handling functions.
getc() and putc() function AIGIRS
Name of Institution
AIGIRS
Name of Institution

User Defined
Functions
User Defined Functions AIGIRS
Name of Institution

1. C allow programmers to define functions. Such


functions created by the user are called user-defined
functions.
2. Depending upon the complexity and requirement of
the program, we can create as many user-defined
functions as you want.
Function Declaration in C AIGIRS
Name of Institution

There are 3 aspects in each C function. They are:


1. Function declaration or prototype This informs compiler
about the function name, function parameters and return
values data type.
2. Function call This calls the actual function
3. Function definitionThis contains all the statements to be
executed.

1. A function prototype is simply the declaration of a function that specifies


function's name, parameters and return type. It doesn't contain function body.
2. A function prototype gives information to the compiler that the function may
later be used in the program.
How User Defined Functions Works AIGIRS
Name of Institution
How User Defined Functions Works AIGIRS
Name of Institution

Remember, function name is an identifier and should be unique.


Function Introductions AIGIRS
Name of Institution

1. A function declaration tells the compiler about a


function's name, return type, and parameters.
2. A function definition provides the actual body of the
function.
3. The C standard library provides numerous built-in
functions that your program can call.
For example, strcat() to concatenate two strings, memcpy() to copy one
memory location to another location, and many more functions.

4. A function can also be referred as a method or a sub-


routine or a procedure, etc.
Function Definition AIGIRS
Name of Institution

A function definition in C programming consists of a function


header and a function body. Here are all the parts of a function
1. 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.
2. Function Name This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
3. 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.
4. Function Body The function body contains a collection of statements that
define what the function does.
Function Declaration AIGIRS
Name of Institution

1. A function declaration tells the compiler about a function name


and how to call the function.
2. The actual body of the function can be defined separately.
A function declaration has the following parts
Sub-Parts of Function AIGIRS
Name of Institution
Function Declaration/Definition AIGIRS
Name of Institution
Function Definition AIGIRS
Name of Institution

The general form of a function definition in C


programming language is as follows

A function definition in C programming consists of a function


header and a function body. Here are all the parts of a function
1. Return Type
2. Function Name
3. Parameters
4. Function Body
Syntax for Declaration/DefinitionName of Institution
AIGIRS
Syntax for Declaration/DefinitionName of Institution
AIGIRS

Note : The return statement forces the function to return immediately though
function is not yet completed
Syntax for Declaration/DefinitionName of Institution
AIGIRS
Example to understand User Defined functions AIGIRS
Name of Institution
Rules of Writing Function in C Programming AIGIRS
Name of Institution

Rule 1. C program is a collection of one or more functions

Rule 2. A function gets called when the function name is followed by a


semicolon.
Rules of Writing Function in C Programming AIGIRS
Name of Institution

Rule 3 : A function is defined when function name is followed by a


pair of braces in which one or more statements may be present.

Rule 4. Any function can be called from any other function.( main also )
Rules of Writing Function in C Programming AIGIRS
Name of Institution

Rule 5.A function can be called any number of times.

Rule 6. The order in which the functions are defined in a program and
the order in which they get called need not necessarily be same
Rules of Writing Function in C Programming AIGIRS
Name of Institution

Rule 7. A function can call itself ( Process of Recursion )

Rule 8. A function can be called from other function, but a function


cannot be defined in another function.
AIGIRS
C Programming Function Calling Types Name of Institution

There are different types of function calling. Depending on the number of


parameters it can accept , function can be classified into following 4 types:
Type 1 : Accepting Parameter and Returning Value
Name AIGIRS
of Institution

Function accepts argument and it return a value back to the calling Program thus it can
be termed as Two-way Communication between calling function and called function.
Type 1 : Accepting Parameter and ReturningName AIGIRS
Valueof Institution
Type 2 : Accepting Parameter and Not Returning Value
Name AIGIRS
of Institution

1. Function accepts argument but it does not return a value back to the calling
Program .
2. It is Single ( One-way) Type Communication
3. Generally Output is printed in the Called function
4. Here area is called function and main is calling function.
Type 2 : Accepting Parameter and Not Returning Value
Name AIGIRS
of Institution

1. Function accepts argument but it does not return a value back to


the calling Program .
2. It is Single ( One-way) Type Communication
3. Generally Output is printed in the Called function
4. Here area is called function and main is calling function.
Type 3 : Not Accepting Parameter but Returning Value
Name AIGIRS
of Institution
Type 4 : Not Accepting Parameter and Not Returning Value
Name AIGIRS
of Institution
Type 4 : Not Accepting Parameter and Not Returning Value
Name AIGIRS
of Institution
Function Declaration in C AIGIRS
Name of Institution

There are 3 aspects in each C function. They are:


1. Function declaration or prototype This informs compiler
about the function name, function parameters and return
values data type.

2. Function call This calls the actual function


3. Function definitionThis contains all the statements to be
executed.

1. A function prototype is simply the declaration of a function that specifies


function's name, parameters and return type. It doesn't contain function body.
2. A function prototype gives information to the compiler that the function may
later be used in the program.
Calling a Function AIGIRS
Name of Institution

1. While creating a C function, you give a definition of what the


function has to do. To use a function, you will have to call that
function to perform the defined task.
2. 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.
3. To call a function, you simply need to pass the required parameters
along with the function name, and if the function returns a value,
then you can store the returned value.
Ways of Passing Argument to Function Name AIGIRS
in C of Institution

There are two ways of Passing Argument to Function in


C Language :
1.Call by value
2.Call by Reference/Pointer/Address
Call by Value AIGIRS
Name of Institution

1. In call by value method, the value of the variable is passed to the


function as parameter.
2. The value of the actual parameter can not be modified by formal
parameter.
3. Different Memory is allocated for both actual and formal
parameters. Because, value of actual parameter is copied to formal
parameter.
Note:
a. Actual parameter This is the argument which is used in function call.
b. Formal parameter This is the argument which is used in function
definition

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.
Call by Value AIGIRS
Name of Institution

1) While Passing Parameters using call by value,


Xerox copy of original parameter is created and
passed to the called function.
2) Any update made inside method will not affect
the original value of variable in calling function.
3) In the above example num1 and num2 are the
original values and Xerox copy of these values is
passed to the function and these values are copied
into number1,number2 variable of sum function
respectively.
4) As their scope is limited to only function so
they cannot alter the values inside main
function.
Call by Value AIGIRS
Name of Institution
Call by Value-Example AIGIRS
Name of Institution
Call by Reference AIGIRS
Name of Institution

1. In call by reference method, the address of the variable


is passed to the function as parameter.
2. The value of the actual parameter can be modified by
formal parameter.
3. Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
This method copies the address of an argument into the formal
parameter. 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.
Call by Reference AIGIRS
Name of Institution

1. While passing parameter using call by


address scheme , we are passing the actual
address of the variable to the called
function.
2. Any updates made inside the called
function will modify the original copy since
we are directly modifying the content of the
exact memory location.
Call by Reference AIGIRS
Name of Institution
Call by Reference-Example AIGIRS
Name of Institution

1. The call by reference method of passing arguments to a function


copies the address of an argument into the formal parameter.
2. Inside the function, the address is used to access the actual
argument used in the call. It means the changes made to the
parameter affect the passed argument.
3. To pass a value by reference, argument pointers are passed to the
functions just like any other value.
4. So accordingly we need to declare the function parameters as
pointer types as in the following function swap(), which exchanges
the values of the two integer variables pointed to, by their
arguments.
Call by Reference-Example AIGIRS
Name of Institution
Call by Reference-Example AIGIRS
Name of Institution

Let us now call the function swap() by passing values by reference as


in the following example

1. In this program, the address


of the variables m and n
are passed to the function
swap.
2. These values are not copied
to formal parameters a and
b in swap function.
3. Because, they are just
holding the address of those
variables.
4. This address is used to
access and change the
values of the variables.
Call by Reference-Example AIGIRS
Name of Institution
Summary of Call By Value and Reference AIGIRS
Name of Institution
Ways of Calling a Function AIGIRS
Name of Institution
Ways of Calling a Function AIGIRS
Name of Institution
Ways of Calling a Function AIGIRS
Name of Institution
Passing Argument to Function AIGIRS
Name of Institution

1. In C Programming we have different ways of parameter


passing schemes such as Call by Value and Call by
Reference.
2. Function is good programming style in which we can write
reusable code that can be called whenever require.
3. Whenever we call a function then sequence of executable
statements gets executed. We can pass some of the
information to the function for processing called argument.
Advantages of Functions in C AIGIRS
Name of Institution

1. Modular and Structural Programming can be done


a) We can divide c program in smaller modules.
b) We can call module whenever require. e.g. suppose we
have written calculator program then we can write 4
modules (i.e. add, sub, multiply, divide)
c) Modular programming makes C program more readable.
d) Modules once created , can be re-used in other
programs.
2. It follows Top-Down Execution approach , So main can be
kept very small.
a) Every C program starts from main function.
b) Every function is called directly or indirectly through
main.[See : How to call a function (Simple Example) ]
c) Example : Top down approach. (functions are executed
from top to bottom)
Advantages of Functions in C AIGIRS
Name of Institution

3. Individual functions can be easily built,tested


a) As we have developed C application in modules we can test each
and every module.
b) Unit testing is possible.
c) Writing code in function will enhance application development
process.
4. Program development become easy
5. Frequently used functions can be put together in the customized
library
a) We can put frequently used functions in our custom header file.
b) After creating header file we can re use header file. We can include
header file in other program.
6. A function can call other functions & also itself
a) Function can call other function.
b) Function can call itself , which is called as recursive function.
c) Recursive functions are also useful in order to write system functions.
d) 7. It is easier to understand the Program topic
e) We can get overall idea of the project just by reviewing function
names.
Size of a Function AIGIRS
Name of Institution

Calculate Size of Function in C Program


AIGIRS
Name of Institution

Recursion
Recursion in C AIGIRS
Name of Institution

1. Recursion is the process of repeating items in a self-similar way.


2. In programming languages, if a program allows you to call a
function inside the same function, then it is called a recursive call
of the function.
3. Recursion is the process of repeating items in a self-similar way.
4. In programming languages, if a program allows you to call a
function inside the same function, then it is called
a recursive call of the function. void recursion() { recursion();
/* function calls itself */ } int main() { recursion(); }
Recursion in C AIGIRS
Name of Institution

1. Recursion is basic concept in Programming.


2. When Function is defined in terms of itself then it is called as
Recursion Function.
3. Recursive function is a function which contains a call to itself.

Warning of using recursive function in C Programming :


1. Recursive function must have at least one terminating condition that
can be satisfied.
2. Otherwise, the recursive function will call itself repeatedly until the run
time stack overflows
Recursion in C AIGIRS
Name of Institution

1. The C programming language supports recursion, i.e., a function


to call itself.
2. But while using recursion, programmers need to be careful to
define an exit condition from the function, otherwise it will go into
an infinite loop.
3. Recursive functions are very useful to solve many mathematical
problems, such as calculating the factorial of a number,
generating Fibonacci series, etc.
Recursion in C AIGIRS
Name of Institution
Recursion in C AIGIRS
Name of Institution

unsigned int factorial(unsigned int a); unsigned int factorial


(unsigned int a) {
int main () {
unsigned int f,x; if (a==1)
printf("Enter value between 1 & 8: ");
scanf("%d", &x); return 1;
if (x > 8 || x < 1)
else {
printf (Illegal input!\n");
else { a *= factorial(a-1);
f = factorial(x);
printf ("%u factorial equals %u\n", return a;
x,f);
} }
} }
Recursion in C AIGIRS
Name of Institution

Example: Sum of Natural Numbers Using Recursion


Recursion in C AIGIRS
Name of Institution
Recursion-Number Factorial AIGIRS
Name of Institution

The following example calculates the factorial of a given number using a


recursive function .
Recursion-Fibonacci Series AIGIRS
Name of Institution

The following example generates the Fibonacci series for a given


number using a recursive function
Recursion in C AIGIRS
Name of Institution

Advantages and Disadvantages of Recursion


1. Recursion makes program elegant and cleaner. All
algorithms can be defined recursively which makes it
easier to visualize and prove.
2. If the speed of the program is vital then, you should
avoid using recursion. Recursions use more memory
and are generally slow. Instead, you can use loop.

Das könnte Ihnen auch gefallen