C Programming Module-VIII
C Programming Module-VIII
C PROGRAMMING
Functions
B.Tech. – CSE
Academic Year : 2023-24
Term – II
Compiled by Er. K. Ravikanth, Assistant
1/27/2025 1
Professor, Dept. of CSE, AU
OUT LINE
Functions
Declaring a function
Signature of a function
Call by value
Recursion
Simple programs
Finding Factorial
Fibonacci series
main( )
{
// Block of statements
}
Compiled by Er. K. Ravikanth, Assistant
1/27/2025 4
Professor, Dept. of CSE, AU
Designing structured programs
Syntax:
Eg:
1.Function name
3.List of parameters
5.Function statements
1.Function Header
2.Function Body
Executable statement1;
-----------------------
-----------------------
Retrun type
• x is “Formal Parameter“
• n is “Actual Parameter“
Compiled by Er. K. Ravikanth, Assistant
1/27/2025 11
Professor, Dept. of CSE, AU
Types of Parameter Passing
• There are two ways to pass value or data to function in C
language: call by value and call by reference. Original value is
not modified in call by value but it is modified in call by
reference.
scanf("%d%d",&x,&y); a = temp;
1.Call-by-Value
2.Call-by-Reference
Call-by-Value:
Note: If you made any changes on formal parameters that does not
reflect on the actual parameters.
Compiled by Er. K. Ravikanth, Assistant
1/27/2025 16
Professor, Dept. of CSE, AU
void main()
{
int x=10, y=20;
clrscr();
void swap(int, int);
printf("x,y values before swapping:x=%d,y=%d",x,y);
swap(x,y);
printf("x,y values after swapping: x=%d,y=%d",x,y);
}
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
Output:
x,y values before swap:x=10, y=20
x,y values after swap:x=20, y=10
Note:
In the above program x,yCompiled
are actual parametrs
by Er. K. and a,b are formal/dummy parameters
Ravikanth, Assistant
1/27/2025 17
Professor, Dept. of CSE, AU
Call-by-Reference
int i;
for(i=0;i<n;i++)
printf("%d\t",*(ptr+i));
Output:
2. Predefined functions
Predefined functions:
E.g.: printf(),scanf(),pow(x,y), sqrt(n),ceil(),floor(),round()
Eg: main()
4!=4x3x2x1
4!=4x3!
3!=3x2!
2!=2x1!
1!=1x0!
0!=1
4!=4x3! (6x4=24)
3!=3x2!(3x2=6)
2!=2x1!(2x1=2)
1!=1x0!(1x1=1)
Compiled by Er. K. Ravikanth, Assistant
1/27/2025 25
Professor, Dept. of CSE, AU
Contd..
Program
int factorial(int); {
{ return 1;
scanf("%d",&n); }
f=factorial(n);
printf("%d",f);
}
Compiled by Er. K. Ravikanth, Assistant
1/27/2025 26
Professor, Dept. of CSE, AU
Fibonacci Sequence using recursion
int fib(int);
void main()
{
int n,i;
clrscr();
printf("Enter the number");
scanf("%d",&d);
for(i=0;i<=n,i++)
{
printf("%d",fib(n));
}
getch();
}
int fib(int n)
{
if(n<=1)
return n;
else
return fib(n-1) + fib(n-2);
} 1/27/2025 Compiled by Er. K. Ravikanth, Assistant
27
Professor, Dept. of CSE, AU
Limitations of Recursion
– Parameters (arguments)
– return type.
• Formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
• While calling a function, there are two ways in which arguments can be passed to a
function −
Sr.No. Call Type & Description
1 Call by valueThis method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter inside the
function have no effect on the argument.
2 Call by referenceThis 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.
int b[10][20];
float marks[10];
• Types of recursion:
– 1. Direct
– 2. Indirect
Example:
int func1(int n)
{
if (n<=1)
return 1;
else
return func2(n);
}
int func2(int n)
{ return func1(n);
}
• In this program, func1() calls func2(), which is a new function. But this new
function func2() calls the first calling function, func1(), again. This makes the above
function an indirect recursive function.
• Disadvantages:
– Slow speed.
• Its value grows rapidly, even for small inputs. For example, A(4, 2) is an integer of
19,729 decimal digits.
2. https://www.geeksforgeeks.org/recursion/
3. https://beginnersbook.com/2014/01/c-passing-array-to-function-
example/
4. https://www.tutorialspoint.com/cprogramming/c_passing_arrays_
to_functions.htm