Programming in C
Module IV
Functions
Functions
Functions
A function is a group of statements that together perform a task.
• You can divide up your code into separate functions. Logically
the division is such that each function performs a specific task.
• A function can also be referred as a method or a sub-routine
or a procedure.
3
Advantages of using Functions
• Avoid repetition of codes.
• Increases program readability.
• Divide a complex problem into simpler
ones.
• Reduces chances of error.
• Modifying a program becomes easier by
using function.
4
Elements of User Defined Function
1. Function Prototype (function declaration)
2. Function Definition
3. Function Call
5
How function works ?
6
1. Function Declaration
• A function declaration tells the compiler about a function
name and how to call the function.
• Syntax:
returntype function_name (Parameter List);
• Example:
1.
2.
7
[Link] Definition
• Contains all the statements to be executed.
returntype function_name (parameter list)
{
//body of the function
}
8
Example
9
3. Function Call
• When a program calls a function, the program control
is transferred to the called function.
function_name(arguments list);
• Eg:
m= max(a,b);
m=max(5,3);
10
#include <stdio.h> // Function to calculate the factorial of a number
// Function declarations
int factorial(int num) {
int factorial(int num); int fact = 1;
int nCr(int n, int r); for (int i = 1; i <= num; i++) {
fact *= i;
int main() { }
int n, r, result;
return fact;
printf("Enter value for n: "); }
scanf("%d", &n);
printf("Enter value for r: "); // Function to calculate nCr (combination)
scanf("%d", &r); int nCr(int n, int r) {
// Call the nCr function
return factorial(n) / (factorial(r) * factorial(n - r));
result = nCr(n, r); }
// Display the result
printf("%dC%d = %d\n", n, r, result);
return 0;
}
11
Formal and Actual Parameters
“Parameters" are the information
that is passed to a function.
• Actual parameters are
parameters as they appear in
function calls.
• Formal parameters are
parameters as they appear in
function defnition.
12
Example
#include<stdio.h>
int max(int num1,int num2); // Function declaration or prototype
void main()
{
int a=100,b=200,ret;
ret=max(a,b); //Function Call
printf(“the max value is %d”,ret);
}
//Function Definition
int max(int num1,int num2)
{
int result;
if(num1>num2)
result=num1;
else
result=num2;
return result;
}
13
Categories of User-defined Functions
1. Function with no arguments and no return value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and a return value
14
1. Function with no arguments and no return value
Function Call
15
2. Function with no arguments and a return value
16
3. Function with argument and no return value
17
4. Function with argument and return value
18
Methods of calling a Function
• Call by Value
• Call by Reference
19
Call by value method
• In call by value method, the value of the variable is passed to
the function as parameter.
• The value of the actual parameter can not be modified by
formal parameter.
• Different Memory is allocated for both actual and formal
parameters.
20
Example
#include<stdio.h>
// function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d \n and n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
21
m n
10 20
a b
20 10
22
Programs
• Write a program to find the area of a circle by
creating a function for finding area.
• Write a program to find the factorial of a number
using function
• Write a program to calculate nCr.
23