0% found this document useful (0 votes)
22 views

Unit 5

Physics sem-1 hhshejakmamavxb Hshsjsjskskskjdnsmsjdnbdhdhduekkwkakkalakdbxnbxbdndjdjskskskjhdgxbbxbxbxuywywoqopskjdbdbdbdndndjajkakkwuusuyryujajjdydykqpapooo&578iiudujsopkksn'ggu&57({=π|πYwuiqokjdhejsnnjjsjkkss Sushjsjsnshsjsnmeppqowijrnndbfywoqlksndndbyduipsjndhdugbsnskeojajjsjhejwuwjkqjsjghbsyhsbshysheb6727b!! 'Bayuiekkqkkakjgvsvdvvahqkakkanvzvbdbzhjslqlljhbzbxgdhhshshdbdbdbhsbsbshsbnsjzjnshzjznbzbuznsnsjznsjznnspqpururbdbdnhzisnnsusiwnqppudhdbbd

Uploaded by

a81804432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Unit 5

Physics sem-1 hhshejakmamavxb Hshsjsjskskskjdnsmsjdnbdhdhduekkwkakkalakdbxnbxbdndjdjskskskjhdgxbbxbxbxuywywoqopskjdbdbdbdndndjajkakkwuusuyryujajjdydykqpapooo&578iiudujsopkksn'ggu&57({=π|πYwuiqokjdhejsnnjjsjkkss Sushjsjsnshsjsnmeppqowijrnndbfywoqlksndndbyduipsjndhdugbsnskeojajjsjhejwuwjkqjsjghbsyhsbshysheb6727b!! 'Bayuiekkqkkakjgvsvdvvahqkakkanvzvbdbzhjslqlljhbzbxgdhhshshdbdbdbhsbsbshsbnsjzjnshzjznbzbuznsnsjznsjznnspqpururbdbdnhzisnnsusiwnqppudhdbbd

Uploaded by

a81804432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

303105104 - Computational Thinking for

Structured Design-1
Mr. Pathan Bilalkhan R. Assistant Professor
Department of Computer Science & Engineering
CHAPTER-5
Functions & Recursion
Contents

1. Functions
2. Recursion
1. Function :

● A function in C is a set of statements that when called perform


some specific task.
● It is the basic building block of a C program that provides modularity
and code reusability. The programming statements of a function are
enclosed within { } braces, having certain meanings and performing
certain operations.
● They are also called subroutines or procedures in other languages.
Syntax of Functions in C

The syntax of function can be divided into 3 aspects:


Function Declaration
Function Definition
Function Calls
Function Declarations The parameter name is not mandatory while declaring functions.
We can also declare the function without using the name of the
● In a function declaration, we must data variables.
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);
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).
return_type function_name
A C function is generally defined and declared in a (para1_type para1_name, para2_type
single step because the function definition always para2_name)
starts with the function declaration so we do not {
need to declare it explicitly. The below example // body of the function
serves as both a function definition and a }
declaration.
Structure :
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.
Working concept :

Note: Function call is neccessary to


bring the program control to the
function definition. If not called, the
function statements will not be
executed.
Example:
: // C program to show function // Driver code
// call and definition int main()
#include <stdio.h> {
// Calling sum function and
// storing its value in add variable
// Function that takes two parameters int add = sum(10, 30);
// a and b as inputs and returns
// their sum printf("Sum is: %d", add);
return 0;
int sum(int a, int b) }
{
return a + b;
}
● Output
● Sum is: 40
● As we noticed, we have not used explicit function declaration.
We simply defined and called the function.
Function Return Type

Function return type tells what type of value is returned after all function is executed. When we don’t want to return a
value, we can use the void data type.

Example:

int func(parameter_1,parameter_2);

The above function will return an integer value after running statements inside the function.
Note: Only one value can be returned from a C function. To return multiple values, we have to use pointers or structures.
Function Arguments:
Function Arguments (also known as Function Parameters) are the data that is passed to a function.

Example:

int function_name(int var1, int var2);

Conditions of Return Types and Arguments


In C programming language, functions can be called either with or without arguments and might return
values.
They may or might not return values to the calling functions.
Function with no arguments and no return value
Function with no arguments and with return value
Function with argument and with no return value
Function with arguments and with return value
How Does C Function Work?
Working of the C function can be broken into the following steps as mentioned below:
Declaring a function: Declaring a function is a step where we declare a function. Here we define
the return types and parameters of the function.
Defining a function:
Calling the function: Calling the function is a step where we call the function by passing the
arguments in the function.
Executing the function: Executing the function is a step where we can run all the statements
inside the function to get the final result.
Returning a value: Returning a value is the step where the calculated value after the execution of
the function is returned. Exiting the function is the final step where all the allocated memory to
the variables, functions, etc is destroyed before giving full control to the main function.
Types of Functions
There are two types of functions in C:
• Library Functions
• User Defined Functions
1. Library Function
Flowchart of For Loop
A library function is also referred to as a “built-in function”. A compiler package
already exists that contains these functions, each of which has a specific meaning
and is included in the package. Built-in functions have the advantage of being
directly usable without being defined, whereas user-defined functions must be
declared and defined before being used.

For Example:

pow(), sqrt(), strcmp(), strcpy() etc.


Example: for loop

Advantages of C library functions


C Library functions are easy to use and optimized for better performance.
C library functions save a lot of time i.e, function development time.
C library functions are convenient as they always work.
Library Function
// C program to implement
// the above approach printf("The Square root of %.2lf = %.2lf",
#include <math.h> Number, squareRoot);
#include <stdio.h> return 0;
}
// Driver code
int main()
{
double Number;
Output
Number = 49;
The Square root of 49.00 = 7.00
// Computing the square root with
// the help of predefined C
// library function
double squareRoot = sqrt(Number);
2. User Defined Function

• Functions that the programmer creates are known as User-Defined functions or “tailor-made functions”.
User-defined functions can be improved and modified according to the need of the programmer.
• Whenever we write a function that is case-specific and is not defined in any header file, we need to
declare and define our own functions according to the syntax.

Advantages of User-Defined Functions

• Changeable functions can be modified as per need.


• The Code of these functions is reusable in other programs.
• These functions are easy to understand, debug and maintain.
User Defined Function Example:
// C program to show
// user-defined functions
#include <stdio.h> Output
Sum is: 70
int sum(int a, int b)
{
return a + b;
}

// Driver code
int main()
{
int a = 30, b = 40;

// function call
int res = sum(a, b);

printf("Sum is: %d", res);


return 0;
}
Passing Parameters to Functions

• The data passed when the function is being invoked is known as the Actual parameters.
• In the below program, 10 and 30 are known as actual parameters.
• Formal Parameters are the variable and the data type as mentioned in the function
declaration.
• In the below program, a and b are known as formal parameters.
Example:
Advantages of Functions in C

Functions in C is a highly useful feature of C with many advantages as mentioned


below:

• The function can reduce the repetition of the same statements in the program.

• The function makes code readable by providing modularity to our program.

• There is no fixed number of calling functions it can be called as many times as you
want.
• The function reduces the size of the program.

Once the function is declared you can just use it without thinking about the internal
working of the function.
Disadvantages of Functions in C

The following are the major disadvantages of functions in C:

• Cannot return multiple values.


• Memory and time overhead due to stack frame allocation and transfer
of program control.
Types of User Defined Functions in C

The following are the major types of User Defined Functions in C:

• Function with No Arguments & No Return Type


• Function with No Arguments & But Return Type
• Function with Arguments & No Return Type
• Function with Arguments & Return Type

EXPLAIN ALL OF THEM WITH PROPER EXAMPLE


Recursion:
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program
allows you to call a function inside the same function, then it is called a recursive call of the function.

void recursion() {
recursion(); /* function calls itself */
}

int main() {
recursion();
}
The C programming language supports recursion, i.e., a function to call itself. But while
using recursion, programmers need to be careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc.

Number Factorial
The following example calculates the factorial of a given number using a
recursive function
#include <stdio.h>

unsigned long long int factorial(unsigned int i) {

if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}

int main() {
int i = 12;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}

You might also like