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

Introduction To Function

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. It describes the key parts of a function like the function signature, parameters, return type, and body. It also covers topics like calling functions, passing parameters by value and reference, and returning values from functions. Functions allow modularity, reusability, improved readability and testability of code. Examples are provided to illustrate recursive functions, inline functions, passing arrays to functions, and returning arrays from functions.

Uploaded by

aamirneyazi12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Introduction To Function

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. It describes the key parts of a function like the function signature, parameters, return type, and body. It also covers topics like calling functions, passing parameters by value and reference, and returning values from functions. Functions allow modularity, reusability, improved readability and testability of code. Examples are provided to illustrate recursive functions, inline functions, passing arrays to functions, and returning arrays from functions.

Uploaded by

aamirneyazi12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Dr.

Pawan Kumar Verma


INTRODUCTION TO FUNCTION Associate Professor
CSE Department
INTRODUCTION
A function is a block of code that performs a specific task or set of tasks.

Function Structure

Function
Function signature Function name Function body
parameters

Function Signature: The function signature includes the return type, the function name, and the
parameters (if any). The return type specifies the type of value the function will return, if any.
The function name is a unique identifier for the function within the program. The parameters
(also called arguments) are values that can be passed to the function for it to work with.

Function Body: This is the block of code enclosed within curly braces {}. It contains the
statements and instructions necessary to perform the desired task. When the function is called,
the code within the function body is executed.
ANATOMY OF A C FUNCTION
1. Function Signature

Return Type: The return type of a function specifies the type of value that the function will
return when it completes its execution. It can be any valid C data type, such as int, float, char,
void, or a user-defined data type. If the function doesn't return a value, you use the void
keyword as the return type.

Function Name: The function name is a unique identifier for the function within your program.
It follows the rules of C identifier naming, meaning it can include letters, digits, and underscores
but must start with a letter or underscore.
Parameters (if any): Parameters, also known as function arguments, are variables or values
that you pass to the function for it to work with. They are enclosed within parentheses ()
following the function name. Parameters allow you to send data to the function, which the
function can then use for its operations. If a function doesn't require any parameters, you leave
the parentheses empty.
ANATOMY OF A C FUNCTION
2. Function Body
The function body is enclosed within curly braces {} and contains the actual code that defines
the function's behavior. It includes a sequence of statements, declarations, and control
structures that perform a specific task or set of tasks.

The function body is where you write the logic of the function, specifying what it should do. It
can use the parameters passed to it, perform calculations, make decisions with control
structures (such as if statements and loops), and return a value using the return statement (if
the function has a non-void return type).
CALLING A FUNCTION
1. Calling a Function:

• To call a function in C, you simply use the function's name followed by a pair of parentheses
(). For example, if you have a function named add, you would call it by writing add().

2. Passing Arguments:

• When calling a function, you may need to pass arguments to it. Arguments are values or
variables that the function expects to receive and use for its operations. These arguments
are enclosed within the parentheses when calling the function.
• The number and type of arguments must match the function's parameter list as defined in
its signature. If a function expects two integer arguments, you should provide two integer
values when calling it.
CALLING A FUNCTION
3. Receiving the Result:

• If a function has a return type other than void, it will produce a result that can be used in
your code. To capture and store this result, you assign it to a variable.

• When you call a function that returns a value, you can use the assignment operator (=) to
assign the function call's result to a variable.
ROLE OF FUNCTIONS
Functions allow you to divide a large and complex program into smaller,
more manageable modules. Each function can encapsulate a specific set of
Modularity
tasks, making the code more organized and understandable. This modular
approach simplifies the development and maintenance of the program.

Functions can be reused in multiple parts of a program or even in different


programs. Once you've defined a function to perform a specific task, you can
Reusability
call it whenever you need that task to be executed. This reduces code
duplication and promotes efficiency.

By breaking down a program into smaller functions, each with a well-defined


purpose, the code becomes more readable. Functions act as high-level
Readability
abstractions, and you can understand the program's logic by examining the
function names and their interactions.
ROLE OF FUNCTIONS (CONT.)
Functions make it easier to test and debug a program. You can isolate and
Testing and
test individual functions, making it simpler to identify and fix errors or issues.
Debugging
This modular approach saves time and effort in the development process.

In collaborative programming environments, multiple developers can work


on different functions simultaneously without interfering with each other.
Collaboration
This parallel development is made possible because of the modular structure
of functions.
EXAMPLE
TYPES OF FUNCTION
These functions are provided by the C standard library and
Standard can be used in any C program. Examples include printf(),
Library scanf(), malloc(), and strlen(). They are typically included by
Functions including the appropriate header files, like <stdio.h> and
<stdlib.h>.

User-Defined These are functions created by the programmer to perform


Functions specific tasks.
TYPES OF FUNCTION
Recursive
Inline Functions
Functions

Inline functions are a C feature that suggests to


the compiler to insert the code of the function at
A recursive function is a function that
the point where it is called, rather than
calls itself. These functions are often used
performing a function call. This can improve
to solve problems that can be broken
performance for small, frequently called
down into smaller, similar subproblems.
functions. They are defined using the inline
keyword.
EXAMPLE OF RECURSIVE FUNCTION
EXAMPLE OF RECURSIVE FUNCTION
EXAMPLE OF INLINE FUNCTION
EXAMPLE OF INLINE FUNCTION
PASSING PARAMETERS TO FUNCTION: CALL BY
VALUE & CALL BY REFERENCE
Call By Value

• In the call by value method, a copy of


the value of the actual argument (the
value in the calling function) is passed to
the formal parameter (the parameter in
the called function).
• Any modifications made to the formal
parameter within the function do not
affect the original argument.
• Call by value is suitable for passing
values when you want to ensure that the
original data remains unchanged.
PASSING PARAMETERS TO FUNCTION: CALL BY
VALUE & CALL BY REFERENCE
Call By Reference
(using Pointers)

• In the call by reference method, you pass


a reference (memory address) to the
actual argument. This reference is
typically achieved by using pointers.
• Modifications made to the parameter
inside the function directly affect the
original argument because you are
operating on the same memory location.
• Call by reference is suitable when you
want to modify the original data within
the function.
CALL BY VALUE (EXAMPLE)
CALL BY REFERENCE (EXAMPLE)
PASSING AN ARRAY TO
A FUNCTION

In this example, the sumArray function


takes an integer array and its size as
parameters and returns the sum of the
elements in the array. The main function
passes the numbers array and its size to
the sumArray function.
RETURNING AN ARRAY
FROM A FUNCTION

In this example, the generateSquares


function dynamically allocates an array
to store the squares of the first n
numbers. It returns a pointer to the
allocated array, which is then used in the
main function. After using the array, you
should free the dynamically allocated
memory to prevent memory leaks.
Thank you.. ☺

You might also like