User-defined function
&
Recursion
Class 05
Introduction to Functions
• A function is a block of code which only runs when it is
called.
• You can pass data, known as parameters, into a function.
• Functions are used to perform certain actions, and they are
important for reusing code: Define the code once, and use it
many times.
• A C program must have main( ).
• Without main() function, there is technically no C program.
Types of functions
There are two types of functions in C programming:
• Library function
• User defined function
Library Functions
• Library functions are the in-built function in C programming system.
• In C, we used many library functions - printf (), scanf (), clrscr (), or
getch ()
• printf() prints on the output
• scanf () takes input from the user and assigns that input to a variable
by going to the variable’s address
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1.Function Declaration
2.Function Definition
3.Function Calls
Function Declarations
In a function declaration, we must provide the function name, its return type, and
the number and type of its parameters. A function declaration tells the compiler
that there is a function with the given name defined somewhere else in the
program.
Syntax
return_type name_of_the_function (parameter_1, parameter_2);
The parameter name is not mandatory while declaring functions. We can also declare
the function without using the name of the data variables.
Example
int sum(int a, int b);
int sum(int , int);
Function Definition
•The function definition consists of actual statements which are
executed when the function is called (i.e. when the program control
comes to the function).
•A C function is generally defined and declared in a single step because
the function definition always starts with the function declaration so we
do not need to declare it explicitly. The below example serves as both a
function definition and a declaration.
return_type function_name (para1_type para1_name, para2_type para2_name)
{
// body of the function
}
Function Call
•A function call is a statement that instructs the compiler to execute the
function. We use the function name and parameters in the function call.
•In the below example, the first sum function is called and 10,30 are
passed to the sum function. After the function call sum of a and b is
returned and control is also returned back to the main function of the
program.
Exercise:
Create a function named myFunction and call it
inside main().
#include<stdio.h>
// Create a function
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction(); // call the function
return 0;
}
A function can be called multiple
times:
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
Parameters and Arguments
• Information can be passed to functions as a parameter.
Parameters act as variables inside the function.
• Parameters are specified after the function name, inside the
parentheses. You can add as many parameters as you want,
just separate them with a comma:
returnType functionName(parameter1, parameter2, paramet
er3) {
// code to be executed
}
Example
int myFunction(int x, int y) {
return x + y;
}
int main() {
int result = myFunction(5, 3);
printf("Result is = %d", result);
return 0;
}
Real-Life Example
To demonstrate a practical example of using functions, let's create
a program that converts a value from fahrenheit to celsius:
// Function to convert Fahrenheit to Celsius
float toCelsius(float fahrenheit) {
return (5.0 / 9.0) * (fahrenheit - 32.0);
}
int main() {
float f_value = 98.8;
// Call the function with the fahrenheit value
float result = toCelsius(f_value);
printf("Fahrenheit: %.2f\n", f_value);
printf("Convert Fahrenheit to Celsius: %.2f\n", result);
return 0;
}
Function declaration
// Function declaration
int myFunction(int, int);
// The main method
int main() {
int result = myFunction(5, 3); // call the function
printf("Result is = %d", result);
return 0;
}
// Function definition
int myFunction(int x, int y) {
return x + y;
}
Category of a function
• A function can be categorized depending on argument and return
type—
• Function with no argument and no return value
• Function with argument and no return value
• Function with argument and return value
Function with no argument and no
return value
Function with argument and no return value
Function with argument and no return value
Function with argument and return value
Function with argument and return value
Function with argument and return value
6
Function with argument and return value
Function with argument and return value
6
Function with argument and return value
Advantages of user defined functions
• User defined functions helps to decompose the large program into
small segments which makes programmer easy to understand,
maintain and debug.
• If repeated code occurs in a program. Function can be used to
include those codes and execute when needed by calling that
function.
• Programmer working on large project can divide the workload by
making different functions.
What is Recursion?
• A function that calls itself is known as a recursive
function. And, this technique is known as recursion.
30
Recursion Example
• Adding two numbers together is easy to do, but adding a range of
numbers is more complicated. In the following example, recursion is
used to add a range of numbers together by breaking it down into
the simple task of adding two numbers:
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 +9+8+7+6+5+4+3+2+1+0
Example
int sum(int k);
int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
When the sum() function is called, it adds parameter k to the sum of
all numbers smaller than k and returns the result. When k becomes 0,
the function just returns 0
Recursion
34
35