Sie sind auf Seite 1von 30

Functions

Goutam Majumder

Lovely Professional University


School of Computer Science & Engineering
goutam.23320@lpu.co.in

September 4, 2018

Goutam Majumder (LPU) Short title September 4, 2018 1 / 30


Overview

1 Background
Needs of a Function
Working Flow of a Function
2 The General Form
3 Understanding the Scope of a Function
4 Steps to Create and Use of a Function
5 Types of Function
6 Function Arguments
Call by Value
Call by Reference
7 Function Recursion
direct recursion
indirect recursion

Goutam Majumder (LPU) Short title September 4, 2018 2 / 30


Functions
Needs of a Function

To support Divie and Conquer


It is difficult to prepare and maintain a large–sized program. Moreover, the
identification of the flow of the data is hard to understand. The best way to
prepare a programming application is to divide the bigger program into small
pieces or modules. This method is called the divide and conquer method.

Code Re–usability
If we want to perform a task repetitively, then it not necessary to re–write
the particular block of the program. Put this block of code in a user–defined
function, which can be called any number of times.

Goutam Majumder (LPU) Short title September 4, 2018 3 / 30


Functions
Working Flow of Function

1 Once a function is defined and called, it takes some data from the
calling function and returns a value to the called function.
2 The detail of inner working of function is unknown to the rest of the
progran. Whenever a function is called, control passes to the called
function and working of calling function is paused. When the execution
of called function is completed, control returns back to the calling
function and executes the next statement.
3 The values of the actual arguments passed by the calling function and
received by the formal arguments of the called function. The number
of actual and formal arguments must be same.
4 The function operates on formal arguments and sends back the result
to calling function and return statement performs this task.

Goutam Majumder (LPU) Short title September 4, 2018 4 / 30


Function
The General Form of a C Function

– A function can be without parame-


ters, in which case the parameter list
is empty.
– An empty parameter list can be ex-
General form of a C function plicitly specified as such by placing the
keyword void inside the parentheses.
– The ret–type specifies the type of
data that the function returns. A
function may return any type of data
except an array. The parameter list
is a comma–separated list of vari-
able names and their associated types.
The parameters receive the values of Function without parameter
the arguments when the function is
called.
Goutam Majumder (LPU) Short title September 4, 2018 5 / 30
Functions
Scope of a Function

What is Scope?
The scope rules of a language are the rules that govern whether a piece of
code knows about or has access to another piece of code or data.
So the scope of a function could mean two things: either the scope defined
by the function’s body, in which its local variables are declared; or the scope
(either a class or a namespace) in which the function name is declared.

Important Points Related to Scope of a Function


– Each function is a discrete block of code. Thus, a function defines a block
scope. This means that a function’s code is private to that function and
cannot be accessed by any statement in any other function except through
a call to that function.
N.B.: For instance, you cannot use goto to jump into the middle of
another function
Goutam Majumder (LPU) Short title September 4, 2018 6 / 30
Functions
Scope of a Function

Important Points Related to Scope of a Function


– The code that constitutes the body of a function is hidden from the rest
of the program, and unless it uses global variables, it can neither affect
nor be affected by other parts of the program.
– Stated another way, the code and data defined within one function cannot
interact with the code or data defined in another function because the two
functions have different scopes.

Lifetime of a variable
– Variables that are defined within a function are local variables. A local vari-
able comes into existence when the function is entered and is destroyed
upon exit.
– Thus, a local variable cannot hold its value between function calls.
N.B.: The only exception to this rule is when the variable is declared
with the static storage class specifier.
Goutam Majumder (LPU) Short title September 4, 2018 7 / 30
Functions
Scope of a Function

Points related to Function Parameter


– The formal parameters to a function also fall within the function’s scope.
This means that a parameter is known throughout the entire function.
– A parameter comes into existence when the function is called and is
destroyed when the function is exited.

Function inside another function


All functions have file scope. Thus,
you cannot define a function within a
function. This is why C is not techni-
cally a block-structured language.

Goutam Majumder (LPU) Short title September 4, 2018 8 / 30


Functions
Steps for Function

Steps
To use a function in a C program following steps are followed. Such as,
1 Function Declaration
2 Function Definition
3 Function Call

Function Declaration
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.

Goutam Majumder (LPU) Short title September 4, 2018 9 / 30


Functions
Function Declaration

Function Declaration
– Function declaration is required when you define a function in one source
file and you call that function in another file. In such case, you should
declare the function at the top of the file calling the function.
– Function declaration can be place inside main method or outside of the
main method, but before function call and definition.

Goutam Majumder (LPU) Short title September 4, 2018 10 / 30


Functions
Steps for Function Definition

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 2. Function Name
return type is the data type of the This is the actual name of the
value the function returns. Some function. The function name and
functions perform the desired opera- the parameter list together consti-
tions without returning a value. In tute the function signature.
this case, the return type is the key-
word void.

Goutam Majumder (LPU) Short title September 4, 2018 11 / 30


Functions
Steps for Function Definition

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.

Goutam Majumder (LPU) Short title September 4, 2018 12 / 30


Function
Call a Function

Calling a Function
– 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.
– When a program calls a function, the program control is transferred to
the called function and performs the defined task and returns the program
control back to the main program.

Goutam Majumder (LPU) Short title September 4, 2018 13 / 30


Functions
Function Calling

Points to Remember While Calling a Function


– Function name and the number and types of arguments in the function
call must be same as that given in the function declaration and function
header of the function definition.
– If by mistake the parameters passed to a function are more or less than
that what it is specified to accept then Compile Time Error will be thrown.
– Names (and not the types) of variables in function declaration, function
call, and header of function defination may vary.
– Arguments may be passed in the form of expressions to the called function.
In such cases, arguments are first evaluated and converted to the type of
formal parameter and then the body of the function gets executed.
– The parameter list must be separated by commas.
– If the return type of the function is not void, then the value returned by
the called function may be assigned to some variable.

Goutam Majumder (LPU) Short title September 4, 2018 14 / 30


Types of Function
Without arguments and return values

Goutam Majumder (LPU) Short title September 4, 2018 15 / 30


Types of Function
With arguments but without return values

Goutam Majumder (LPU) Short title September 4, 2018 16 / 30


Types of Function
With arguments and return values

Goutam Majumder (LPU) Short title September 4, 2018 17 / 30


Types of Function
Without arguments but return values

Goutam Majumder (LPU) Short title September 4, 2018 18 / 30


Functions
Function Arguments

– If a function is to accept arguments, it must declare the parameters that


will receive the values of the arguments.

– Even though parameters perform the special task of receiving the value
of the arguments passed to the function, they behave like any other local
variable.
– For example, you can make assignments to a function’s formal parameters
or use them in an expression.

Goutam Majumder (LPU) Short title September 4, 2018 19 / 30


Functions
Function Arguments

There are two ways in which we can pass arguments to the function.
Call by Value
This method copies the actual value of an argument into the formal param-
eter of the function. In this case, changes made to the parameter inside the
function have no effect on the argument.

Call by Reference
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.

N.B: 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.
Goutam Majumder (LPU) Short title September 4, 2018 20 / 30
Example of Call by Value (Swap Numbers of two variable)

Goutam Majumder (LPU) Short title September 4, 2018 21 / 30


Ex. of Call by Reference (Swap Numbers of two variable)

Goutam Majumder (LPU) Short title September 4, 2018 22 / 30


Recursion

What is Recursion?
It is a process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.
Recursion is two types:

Two Function calling each other


Function Calling itself (direct)
(in–direct)

Goutam Majumder (LPU) Short title September 4, 2018 23 / 30


Recursion
An Example (Find the Factorial of a number)

Factorial n, denoted n!, is the product of positive integers from 1 to n. The


factorial can be formally defined as:
factorial(0) = 1, (base case)
factorial(n) = n ∗ factorial(n − 1), for n > 0. (recursive call)
Recursion shows up in this definition as we define factrorial(n) in terms of
factorial(n − 1).
– Every recursion function should have termination condition (base case)
to end recursion. In this example, when n = 0, recursion stops. The above
function expressed in C is:

Goutam Majumder (LPU) Short title September 4, 2018 24 / 30


Recursion
How it is works (implementation)?

– Its implementation is not different from implementing other functions. For


function call, each procedure call instance is distinct from the others, the
fact that a recursive function calls itself does not make any big difference.
– Each active procedure maintains an activation record, which is stored on
the stack. The activation record consists of the arguments, return address
(of the caller), and local variables.
– The activation record comes into existence when a procedure is invoked
and disappears after the procedure is terminated and the result is returned
to the caller.
– Thus, for each procedure that is not terminated, an activation record
that contains the state of that procedure is stored. The number of
activation records, and hence the amount of stack space required to run the
program, depends on the depth of recursion.

Goutam Majumder (LPU) Short title September 4, 2018 25 / 30


Recursion
Its Implementation (a diagramatic view)

As you can see from the figure, each call to the factorial creates an activation
record until the base case is reached and starting from there we accumulate
the result in the form of product.
Goutam Majumder (LPU) Short title September 4, 2018 26 / 30
Recursion
Example(Sum of Natural Numbers Using Recursion)

Goutam Majumder (LPU) Short title September 4, 2018 27 / 30


Recursion
Sum of n natural number (intermediate steps)

Goutam Majumder (LPU) Short title September 4, 2018 28 / 30


Recursion

Advantages
Recursion makes program elegant and cleaner. All algorithms can be
defined recursively which makes it easier to visualize and prove.
Using recursion the length of the program can be reduce.

Disadvantages
If the speed of the program is vital then, you should avoid using recur-
sion.
Recursions use more memory and are generally slow. Instead, you can
use loop.
If the programmer forgets to specify the exit condition in the recursive
function, the program will execute out of memory.

Goutam Majumder (LPU) Short title September 4, 2018 29 / 30


The End

Goutam Majumder (LPU) Short title September 4, 2018 30 / 30

Das könnte Ihnen auch gefallen