PPS Chapter 5 Functions
PPS Chapter 5 Functions
UNIT-5
Functions
1
Created by Trushita Patel
Topics to be covered:
• Concepts function • Parameter
• Standard library passing
function
• Calling a
• User defined functions
function
• Prototypes
• Recursive
• Definition of function
• Parameters function
• Macros
• Pre-
Created by Trushita Patel
processing 2
Concepts of Function
• ” A function is a group of statements that together perform a
task.”
• Every C program has at least one function, which is main(),
• you can divide up your code into separate functions.
⮚ Why we need functions in C?
• To improve the readability of code.
• Improves the reusability of the code, same function can be used in
any program rather than writing the same code from scratch.
• Debugging of the code would be easier if you use functions, as errors
are easy to be traced.
Created by Trushita Patel 3
• Reduces the size of the code, duplicate set of statements are replaced
Types of functions
• There are two types of function in C programming:
1) Pre-defined standard library functions
2) User-defined functions
#include <stdio.h>
int main()
{
printf("Catch me if you can.");
}
⮚ Advantages
• use library functions is simply
• The functions are optimized for performance
• It saves considerable development time
Created by Trushita Patel
• The functions are portable 5
Program
• To compute the square root of a number, you can use the sqrt() library
function. The function is defined in the math.h header file.
#include <stdio.h>
#include <math.h>
void main()
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);
root = sqrt(num);
printf("Square root of %.2f = %.2f", num, root);
}
void main()
{
... .. ...
functionName(2,3);
... .. ...
Created by Trushita Patel
} 8
User-defined function
⮚ Advantages of user-defined function
• The program will be easier to understand, maintain and debug.
• Reusable codes that can be used in other programs
• A large program can be divided into smaller modules. Hence, a large
project can be divided among many programmers.
•Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
void introduction()
{
printf("Hi\n");
printf("My college name is gec patan\n");
printf("How are you?");
}
void main()
{
introduction();
}
int main(void)
{
int x = 20;
fun(x);
printf("x = %d", x);
return 0;
} Created by Trushita Patel 16
Program for Pass by Reference:
In C, Parameters are always passed by reference in C. For example. in the
below code, value of x is modified using the function fun().
# include <stdio.h>
void fun(int *(2046))
{
*ptr = 30;
}
int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);
return 0;
} Created by Trushita Patel 17
Different aspects of function
calling
• function without arguments and without return value 🡪 void f1( )
• function without arguments and with return value 🡪 int f1( )
• function with arguments and without return value 🡪 void f1(a)
• function with arguments and with return value 🡪 int f1(a)
}
Created by Trushita Patel 26
Program for Fibonacci Series
#include <stdio.h> void main()
int fibonacci(int i) {
{ int i;
if(i == 0) for (i = 0; i < 10; i++)
{ {
return 0; printf("%d\t\n", fibonacci(i));
} }
if(i == 1) }
{
return 1;
}
return (fibonacci(i-1) +
fibonacci(i-2));
}