Lecture-SP16-Functions-3
Lecture-SP16-Functions-3
Structured Programming
Lecture 16
Functions in C (3)
Prepared by________________________________
Md. Mijanur Rahman, Prof. Dr.
Dept. of Computer Science and Engineering
Jatiya Kabi Kazi Nazrul Islam University, Bangladesh
www.mijanrahman.com
Contents
FUNCTIONS IN C
• Top-Down Modular Programming using Functions
• Functions in C
• Why do we need function?
• Types of Functions
• Elements of User-defined Function
• Function Definition • Function Declaration
• Function Calling
• Category of Functions
• Parameters passing to Functions
• Main Function
• Library Functions
• Nesting of Functions
Functions in C 2
• Recursion
Parameter Passing to Functions
• The parameters passed to function are called actual parameters. The parameters
received by function are called formal parameters.
• There are two most popular ways to pass parameters.
• Pass by Value: In this parameter passing method, values of actual parameters are copied
to function’s formal parameters and the two types of parameters are stored in different
memory locations. So any changes made inside functions are not reflected in actual
parameters of caller.
• Pass by Reference Both actual and formal parameters refer to same locations, so any
changes made inside the function are actually reflected in actual parameters of caller.
Functions in C 3
Parameter Passing to Functions
• Pass by value:
• Parameters are always passed by value in C. For example. in the below code, value of x is not
modified using the function fun().
Functions in C 4
Parameter Passing to Functions
• Pass by reference:
• In C, we can use pointers to get the effect of pass-by reference. For example, consider the below
program. The function fun() expects a pointer ptr to an integer (or an address of an integer). It
modifies the value at the address ptr. The address operator & is used to get the address of a variable
of any data type.
Functions in C 5
Some Important Point About C Functions
• Following are some important points about functions in C:
1. Every C program has a function called main() that is called by operating system when a user runs
the program.
2. Every function has a return type. If a function doesn’t return any value, then void is used as a
return type.
3. In C, functions can return any type except arrays and functions. We can get around this limitation
by returning pointer to array or pointer to function.
4. Empty parameter list in C means that the parameter list is not specified and function can be called
with any parameters. In C, it is not a good idea to declare a function like fun(). To declare a function
that can only be called without any parameter, we should use “void fun(void)”.
5. If in a C program, a function is called before its declaration then the C compiler automatically
assumes the declaration of that function in the following way:
int function name();
Functions in C 6
Main Function
• The main function is a special function. Every C++ program must contain a function named main.
It serves as the entry point for the program. The computer will start running the code from the
beginning of the main function.
• Types of main Function:
1. The first type is – main function without parameters :
// Without Parameters
int main()
{
...
return 0;
}
Functions in C 7
Main Function
2. Second type is main function with parameters :
// With Parameters
intmain(int argc, char * const argv[])
{
...
return 0;
}
• The reason for having the parameter option for the main function is to allow input from
the command line.
Functions in C 8
C Library Functions
• Library functions are the inbuilt function in C that are grouped and placed at a common place
called the library. Such functions are used to perform some specific operations.
• For example, printf is a library function used to print on the console. The library functions are
created by the designers of compilers.
• All C standard library functions are defined inside the different header files saved with the
extension .h.
• We need to include these header files in our program to make use of the library functions
defined in such header files.
• For example, To use the library functions such as printf/scanf we need to include stdio.h in our
program which is a header file that contains all the library functions regarding standard
input/output.
Functions in C 9
C Library Functions
• List of mostly used header files:
Functions in C 10
Example
• Program to calculate the square root of a
number using function.
Functions in C 11
Creating Library Functions
• STEPS FOR ADDING OUR OWN FUNCTIONS IN C LIBRARY:
STEP 1:
• For example, below is a sample function that is going to be added in the C library. Write
the below function in a file and save it as “addition.c”:
addition(int i, int j)
{
int total;
total = i + j;
return total;
}
STEP 3:
• “addition.obj” file would be created which is the compiled form of “addition.c” file.
Functions in C 12
Creating Library Functions
STEP 4:
• Use the below command to add this function to library (in turbo C).
c:\> tlib math.lib + c:\ addition.obj
STEP 5:
• Create a file “addition.h” & declare prototype of addition() function like below.
int addition (int i, int j);
• Now, addition.h file contains prototype of the function “addition”.
• Note : Please create, compile and add files in the respective directory as directory name may
change for each IDE.
Functions in C 13
Creating Library Functions
• STEP 6:
• Let us see how to use our newly added library function in a C program.
Functions in C 14
Nesting of Functions
• In C programming, nesting of functions refers to the practice of defining a function inside
another function.
• This concept is also known as "nested functions" or "inner functions". When a function is
defined within another function, the inner function has access to the variables and parameters
of the outer function.
• However, the outer function cannot access the variables or parameters of the inner function.
• C permits nesting of functions freely. The main() calls function1, which calls function2, which
calls function3, …., and so on. There is in principle no limit as to how deeply functions can be
nested.
Functions in C 15
Nesting of Functions
• A simple example to illustrate nesting of functions in C:
Functions in C 16
Nesting of Functions
• Consider the following ratio and write a
program to calculate the ratio:
𝑥
𝑟=
𝑦−𝑧
Functions in C 17
Recursion
• Recursion in C refers to the process where a function calls itself directly or indirectly. It's
a powerful concept in programming where a problem is solved by dividing it into smaller
sub-problems of the same type.
• Recursion is widely used to solve problems that can be broken down into smaller, similar
sub-problems.
• Recursion has a few key components:
1. Base Case: This is the condition under which the function stops calling itself and returns a result.
Without a base case, the recursion would continue indefinitely, leading to a stack overflow.
2. Recursive Case: This is where the function calls itself with a smaller input, getting closer to the base
case.
3. Stopping Condition: The recursive calls must eventually reach the base case. If not, the recursion
will continue indefinitely.
Functions in C 18
Recursion
• An useful example of recursion is the evaluation of factorials of a given number. The
factorial of a number n is expressed as a series of repetitive multiplications as given
below:
• Factorial of n = n(n-1)(n-2)…..1
• For examples, factorial of 4 = 4x3x2x1 = 24
• Thus, fact of n = n * factorial (n-1)
As n = 3, fact = 3 * factorial (2)
= 3 * 2 * factorial (1)
=3*2*1
=6
Functions in C 19
Recursion
• A function to evaluate factorial of n is as follows:
• Recursive Function factorial:
int factorial(int n){
if(n==0)
return 1;
else
return n*factorial(n-1);
}
Functions in C 20
Recursion
• A function to evaluate
factorial of n:
Functions in C 21
Recursion
• Recursion function to find Fibonacci series: The Fibonacci series is a sequence of numbers where each number
is the sum of the two preceding ones, usually starting with 0 and 1. So, the Fibonacci sequence starts as 0, 1, 1,
2, 3, 5, 8, 13, 21, and so on.
• Recursive Function Fibonacci:
int fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
• Base case: The base case is the simplest form of the problem that can be solved directly without further recursion. In
this case, if n is 0 or 1, the Fibonacci number is n itself. So, if n is less than or equal to 1, the function returns n.
• Recursive case: If n is greater than 1, the Fibonacci number for n is the sum of the Fibonacci numbers for n-1 and n-2.
So, the function recursively calls itself with n-1 and n-2, adds the results, and returns the sum.
Functions in C 22
Recursion
• Recursion function to find Fibonacci series:
Functions in C 23
Assignment and Presentation
On
• The Scope, Visibility and Lifetime of Variables:
• Automatic Variables
• External Variables
• Static Variables
• Register Variables
Functions in C 24
?
Functions in C
THE END
25