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

Lect-11-Function and Return Value

The document discusses different types of functions in C including functions with no input/output, input only, output only, and input/output. It covers passing variables to functions by value and by reference and provides examples of function definitions and recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lect-11-Function and Return Value

The document discusses different types of functions in C including functions with no input/output, input only, output only, and input/output. It covers passing variables to functions by value and by reference and provides examples of function definitions and recursive functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

FUNCTION

AND
FUNCTION TYPES
What is a function
 A block of code that has a name and it has a
property that it is reusable
 A function can be executed as many times at
different points in a C Program as required
 Function groups a number of program statements
into a unit and gives it a name
 Function name is GLOBAL and UNIQUE
 The first function in C is always called ‘main()’
 Function call is the term to use the function
 C function can only return ONE value or NONE
Types of function based on I/O
 Function with no input and no output (*)
 Function with input and no output
 Function with no input and output
 Function with input and output(*)
 Example:
 Check-prime1.c
 Check-prime2.c
Variable passed to a function
 Information passed to a function is called arguments
 There are 3 methods of argument passing in C:
 No passing at all:
no variable is passed the function
 Pass by value:
when the variable is passed, only value of the variable goes
to the function, therefore changing the value in the function
will not effect variable itself
 Pass by reference (usually pointer):
when the reference variable is passed, chancing the value in
the function will effect the variable itself
 Example will follow
Example of a function
int sum(int x, int y)
{
int ans; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans; //return the answer
}

void main()
{
int a=3, b=4, c;
c = sum (a, b);
printf (“result of function sum: %d\n”, c);
a=7;
b=9;
c = sum (a, b);
printf (“result of function sum: %d\n”, c);

}
Variable in a function
 Variable in a function is destroyed after the function
is closed/ return
 Variable is created when the function is called
 Variable can be preserved if you declare the
variable with keyword ‘static’
 Example follows (fucntion-static.c)
Function definition
 Basic method to tell what type the function is
 Function definition must be placed on top of file
 If located on other file, must declare external using
extern keyword
Recursive function
 Recursive function is a function that calls itself
 Can create a very compact and efficient program
 Can be difficult to debug
 Example program is factorial:
 factorial-normal.c
 factorial-recursive.c
Parameter passing
 Variables are passed from calling function into the
function
 Function can work only by variables passed by the
caller
 Two types of parameter passing
 Pass by value
 Pass by reference
Passed by value
 Only value is passed from caller to the function
 Pass by value is the value passed to the function
 When value changed by function, it does not affect
the caller function
Pass by reference
 The reference (memory location) is passed from
caller to function
 When the value is changed in in the function, then it
will affect the caller
 The value changed is the same memory location
referenced by the caller
Note on pass by value and reference
 Beware of this
 This can be very deadly!!!!!
 Programs can cripple into death because of this
 Example follows

You might also like