0% found this document useful (0 votes)
2 views

Function

A function in C is a self-contained program segment that performs a specific task, with at least one function required to be 'main()' for execution to begin. Functions can take arguments and return values, and they can be declared using prototypes before being defined. Recursion is a key concept where a function can call itself, and parameters can be passed by value or by reference.

Uploaded by

Zayed Oyshik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function

A function in C is a self-contained program segment that performs a specific task, with at least one function required to be 'main()' for execution to begin. Functions can take arguments and return values, and they can be declared using prototypes before being defined. Recursion is a key concept where a function can call itself, and parameters can be passed by value or by reference.

Uploaded by

Zayed Oyshik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

What is Function?

• A function is a self-contained program segment that caries out


some specific, well-define task.
• Every C program consists of one(at least) or more functions
• One of these function must be called main()
• Execution of a program always begin by carrying out the
instructions in main().
General Form
#include <header file name>
// function prototype here
int main()
{
/*…………*/
}
return-type f1(parameter list)
{
/*………*/
}
return-type f2(parameter list)
{
/*………*/
}
Simple example

Function Prototype

Program
Function General Form

return-type fun-name(type parameter1, type parameter2)


{
/*……function body…*/
}
Function General Form
return-type fun-name(type parameter1, type parameter2)
{
/*……function body…*/
}
Function Prototype
return-type fun-name(type parameter1, type parameter2)
{
/*……function body…*/
}

return-type fun-name(type parameter1, type parameter2);


Function Prototype
Function Prototype
Function Prototype
Program
Function of Function Prototype
Declare following attributes associated with a function:
• Its return type
• The name of its parameters
• The type of its parameters
• The function name
Function Prototype
• A function prototype declares a function before it is used and prior
to its definition.
• Compiler needs to know this information in order for it to properly
execute a call to function.
• main() does not need prototype.
Argument and Parameter
Argument and Parameter
• A function’s argument is a value that is passed to the function
when the function is called.
• A function can have zero to several argument.
Argument and Parameter
• For function to be able to take arguments, special variables to
receive argument values must be declared, called parameter of the
function.

• The parameters are declared between the parentheses that follow


the function’s name during function definition.

• Functions that take arguments are called parameterized function.


Argument and Parameter
Function Prototype

Function Call Arguments

Parameter
Declaration vs Definition
• A declaration specifies the type of an object.
• A definition causes storage for an object to be created

• In function-
• prototype is declaration
• the function itself, which contain the body of the function
is a definition
Function Call
• We can accessed a function by specifying its name, followed by a
list of arguments enclosed in parentheses and separated by
commas.

• If the function call does not require any arguments, an empty pair
of parentheses must follow the name of the function.
Function Call
• A function will carry out tis intended action whenever it is
accessed (called) from some other portion of the program.
• Once the function has carried out its intended action (end of that
function is reached), control will be returned to the point from
which the function was accessed.
• Traditionally, main function is not called by any other function, but
there is no technical restriction.
The function call
1

3
Function 4
Call
5
Finish!
9
10

6 Start the called function execution

7
8
End of Function
The function call
1

3
Function 4
Call
5
Finish!
9
10

6 Start the called function execution

7
8
End of Function
Function Call
• There are four types of functions and they are:
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with no arguments and return values.
4. Functions with arguments and return values.
Functions with no arguments and no return values
Functions with no arguments and no return values

l Pass
Con t r o
p a s sing
gu ment
No ar

No return va
lu e
Control Pass

Calling Function Called Function


Functions with arguments and no return values
Functions with arguments and no return values

Pass
r o l b)
Con
t
i n g ( a,
tp ass
u m en
ar g

No return value
Control Pass

Calling Function Called Function


Functions with no arguments and return values
Functions with no arguments and return values

l P ass s s ing
tro nt p a
Con rgum
e
n o a

return
Control P value
ass r

Calling Function Called Function


Functions with arguments and return values
Functions with arguments and return values

P ass g (a , b)
tro l as s in
Con m en tp
ar g u

return value
Control Pass r

Calling Function Called Function


Understand Recursion!
• Recursion is the process by which something is defined in
terms of itself.
• In function – Recursion is a process by which a function
calls itself repeatedly, until some specified condition has
been satisfied.
• The process is used for repetitive computations in which
each action is stated in terms of a previous result.
Recursion…
• In order to solve a problem recursively, two conditions
must be satisfied.
• the problem must be written in a recursive form
• the problem include a stopping condition
Example - factorial
• 5! = 5x4x3x2x1
• n! = nx(n-1)x(n-2)x…..2x1

•Using for loop

• again, 5 = 5x4x3x2x1 = 5x(4x3x2x1)


= 5x4! = 5x4x3! = 5x4x3x2! = 5x4x3x2x1
• that is n! = nx(n-1)! Recursion!!!

• Using recursion
Recursion..
• In recursion, no multiple copies of the recursive function
will create. Only one copy exist.
• When a function is called, storage for its parameters and
local data are allocated on the stack.
• Thus, when a function is called recursively, the function
begins executing with a new set of parameter and local
variables, but the function code remain same.
How recursion actually work
• return (n*factorial(n-1));

n! = n*(n-1)! 2! = 2*1
(n-1)! = (n-1)*(n-2)! 3! = 3*2!
(n-2)! = (n-2)*(n-3)! 4! = 4*3!
……………… ………………
……………… ………………
4! = 4*3! (n-2)! = (n-2)*(n-3)!
3! = 3*2! (n-1)! = (n-1)*(n-2)!
2! = 2*1 n! = n*(n-1)!
Home Task
• Write a recursive function that will copy a string to
another. You can’t use strcpy function.
A Closer Look at Parameters
• We can pass arguments to functions in two way

• Call by value
• Call by referance
Call by value
• This method copies the value of an argument into the
formal parameter of the subroutine (function).
• The change made to a parameter of the subroutine have
to no effect on the argument used to call it.
• Default method to call a function
Call by reference
• In this method, the address of an argument is copied into
the parameter.
• Should use pointer
• Inside the subroutine, the address is used to access the
actual argument.
• This means the change made to the parameter will affect
the argument.

You might also like