0% found this document useful (0 votes)
17 views21 pages

Unit 4.2 - Programming in C - Functions

The document provides an overview of functions in C programming, detailing their definitions, advantages, types, and components such as function prototypes, calls, and definitions. It distinguishes between library functions and user-defined functions, explaining their characteristics and use cases. Additionally, it covers storage classes, recursion, and the concept of arguments in functions.

Uploaded by

pakhesaral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views21 pages

Unit 4.2 - Programming in C - Functions

The document provides an overview of functions in C programming, detailing their definitions, advantages, types, and components such as function prototypes, calls, and definitions. It distinguishes between library functions and user-defined functions, explaining their characteristics and use cases. Additionally, it covers storage classes, recursion, and the concept of arguments in functions.

Uploaded by

pakhesaral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Functions

A function is a self-contained program segment or the


block of statements that perform some specific well-
defined task.
Every C program comprises one or more such functions
and any program must contain main() function.
In a program there may be any number of functions.
We need to call the function in order to carry out the
intended task.
A single function can be called from different places
within the program.
After the execution of intended task, the control
returns to where it was called.
Advantages of using function

Increases the reusability of the code.


Program can be divided into multiple functions
and each function can be developed
independently, so program can be developed
easily.
Faster program development.
Easier debugging and understanding of program.
Reduced program complexity and length.
It facilitates top-down modular programming.
The recursive call is possible through function.
Types of function

 Library Function
 Predefined function in a header file or preprocessor directive.
 They cannot change or alter the meaning of this function.
 Eg: printf(), scanf() are defined in header file stdio.h
 getch(), clrscr() are defined in header file conio.h
 strlen(), strcmp() are defined in header file string.h
 pow(), sqrt() are defined in header file math.h
 User-defined Function
 This function is declared, defined, and used by the
programmer according to their need. This function contains
following components:
i. Function declaration (Prototype)
ii. Function call
iii. Function definition (body)
iv. Return statement of function.
Library Function v/s User-defined
Function
Library Function User-defined Function
It is a predefined function in header It is not predefined function; user
file or pre-processor directive. defines this type of function
according to the need.
The programmer can simply use this The has to declare, define and use
function by including the respective this function by themselves.
header file.
The program using library function The program using a user-defined
will be short as the programmer function will be usually lengthy as
doesn’t have to define the function. the programmer has to define the
function.
Program development time will be Program development time will be
faster. usually slower.
The program using the library The program using a user-defined
function will be usually simple. function will be usually complex.
This function requires a header file This function requires a function
to use it. prototype to use it.
Ex: printf(), scanf(), getch(), strrev() Ex: sum(), factorial(), swap_number()
etc etc.
Function Prototype (Function
declaration)

The function prototype provides the following


information:
 It provides the name of the functions.
 It also specifies the type of value returned by
the functions (this is optional and the default
is an integer)
 It also provides the number and type of
argument that must be supplied in the call to
a function.
 Facilitate error checking between calls to a
function and the corresponding function
definition.

When we write the user-defined function ahead of


the main() function, it is not necessary to use the
function prototype.
In ‘top-down’ approach the main() function
appears ahead of user defined function, thus the
function call within main() will precede the
function definition. This can be confusing to
compile unless it is alerted to the fact that function
is called will be defined later in program. Thus, a
function prototype is used for this purpose.
Syntax
return_type function_name(parameter-list);
Function call

A function call is a statement that activates


the execution of the function.
When a function call is encountered in a
program, the control of the program jumps to
the called function, and execution starts.
Syntax
function_name(argument-list);
Function Definition

It provides the actual body of the function.


Contains the statements that will be executed
when then function is called.
Syntax:
return-value-type function-name(parameter-list)
{
declarations and statements
}

Note: Functions cannot be defined inside other


functions.
Return and void statements of a
function

Return Statement
 The return statement terminates the execution of a function and
returns a value to the calling function. The program control is
transferred to the calling function after the return statement.
 Syntax:
return expression;
Void
 If a function doesn’t return then we say the function’s return type
is void meaning that the function doesn’t give output to caller
function. But it may contain a return keyword. If a return keyword
is used, then it is used to transfer control at the end of the
function.
 It needs explanation of pointer.
 Syntax:
void *p;
Actual Argument and Format
Argument

Any variable declared in the body of a function is


said to be local to that function.
If the variables are not declared either-or arguments
or inside function body are considered “global” to
the function and must be defined externally.
Arguments defined inside the function is called
formal argument.
The argument from which the arguments have been
passed to a function is known as actual arguments.
When a function is called, the value of the arguments
is copied to the corresponding parameters in the
function definition.
Categories of User-Defined Function

Function with both arguments and return


values.
Function with arguments but no return
values.
Function without arguments and but with
return values.
Function without any arguments and return
values.
Function with both arguments and return values

This type of function has two way


communication.
An argument is passed from the calling
function to the called function.
There will also be returning statement to the
called function.
Function with arguments and but no return values

This type of function has one-way


communication.
An argument is passed from the calling
function to the called function but there is no
need for a return statement in the called
function.
Function without arguments but with return values

This type of function has one way


communication.
An argument is not passed from the calling
function to the called function, but there is a
need for a return statement in the called
function.
Function without any argument and return values

If we do not want to return a value, we must


use the return type void and miss out on the
return statement.
We simply call a function without passing
arguments and the called function does not
have to return any values to the calling
function.
Concept of storage

The storage class of a variable determines its


scope, its storage duration, and its linkage.
Scope defines where the variable can be
referenced in the program.
Storage defines how long a variable exists in
memory.
Linkage specifies the files in which the
variable is known.

A storage class of a variable defines:


 Where the variable would be stored.
 What will be the initial value of the variable, if the
initial value is not specifically assigned.
 What is the scope of the variable, i.e. in which
functions the value of the variable would be available?
 What is the life of the variable? i.e. how long would
the variable exist?
Note: A storage class of a variable also is determined by
the position of its declaration in the source file and by
the storage class specifications using keywords like
auto, extern, static, or register.
Storage classes

There are four storage classes in the C


language:
1. Automatic storage class
2. External storage class
3. Static storage class
4. Register storage class
Automatic Storage Class

 It is default classification for all variable declared


within a function body. It uses keyword ‘auto’.
 Features:
 Automatic variables are truly local.
 They are unknown to other functions.
 When the function is exited, the values of
automatic variables are not retained.
 They are normally implemented using a stack.
 They are recreated each time the function is called.
 The automatic variable must begin with the class
specifier auto. However, it is optional.
External Storage Class

 The variable of the external storage class is declared


outside any function. These functions are also called global
variables.
 Features:
 External variables are declared outside the function.
 All the functions in program can access and modify global
variables.
 The scope of the variable is global i.e, within the program.
 The life-time of the variable is as long as the program’s
execution does not come to an end.
 The default initial value for this variable is zero.
 An external variable must begin with the class specifier
extern.
Concept of Recursion

A recursive function is one, which call itself.


A recursive function contains the following
features:
 The function should call itself.
 The function should have a stopping
condition (base criteria) and every time the
calls itself it must be closer to the base
criteria.
Note: The concept of using recursive function
is to repeat the execution of statements many
times is known as recursion.

You might also like