0% found this document useful (0 votes)
4 views48 pages

Unit 4 Functions

A function is a self-contained block of code that performs a specific task, allowing for modular programming and code reuse. Functions can be classified into library functions, which are predefined in C's libraries, and user-defined functions, which are created by programmers. The document also explains function declarations, definitions, calls, parameters, and the two common methods of passing parameters: call by value and call by reference.

Uploaded by

rohinijsimr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views48 pages

Unit 4 Functions

A function is a self-contained block of code that performs a specific task, allowing for modular programming and code reuse. Functions can be classified into library functions, which are predefined in C's libraries, and user-defined functions, which are created by programmers. The document also explains function declarations, definitions, calls, parameters, and the two common methods of passing parameters: call by value and call by reference.

Uploaded by

rohinijsimr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 48

Functions

What is Function?
• A function is a small independant unit of
program.

• A function can be defined as a self contained


block of statements which perform a specific
task.
Purpose:
• The use of function allows manageing a large
and complex problems by splitting them into
small and managable parts.

• Function make a block of code reusable.


Types of functions
1. Library functions:
The library functions are the functions which are
already defined in C’s function library i.e. header
files.

For example printf() & scanf() functions are library


functions defined in file stdio.h same as function
sqrt() is defined in math.h and getch() is defined in
conio.h.
User defined functions:
• User defined program is the function defined by the
programmer who has written the program
• The task to be perform is decided by the user.
• for example:the main() function is the user defined
function ,we decide what is to be written in this
function.
How Functions works...?
How Functions works...?
Adventages of function

6. By using function in the program it is possible to


construct modullar and structured programs
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.
• Examples:

int sum(int a, int b); // Function declaration with parameter names

int sum(int , int); // Function declaration without parameter


names
Function prototype
• Like variables,all functions in C program must
be declared before they are used in calling
function.

• This declaration of function known as Function


prototype.
Function Definition
• A function definition is where the actual implementation
(or body) of the function is provided. It includes the
function's declaration as well as the statements that
define what the function does.
• Purpose: The purpose of a function definition is to
provide the code that performs the task or computation
specified by the function.
• Location: A function definition is typically placed after the
function is declared, or at the place in the code where the
function's logic is needed.
Syntax
return_type function_name (para1_type
para1_name, para2_type para2_name)
{
// body of the function
}
Function Declaration Function Definition
Purpose is to inform the compiler
Purpose is to define the function's behavior.
about the function's signature.

No, it does not include the Yes, it includes the full implementation of the
function's body. function.
return_type function_name(parameter_list)
return_type {
function_name(parameter_list); /* body */
}
Typically placed before the function Placed where the function's actual code is
is called. written.

int add(int a, int b)


{
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.
• A statement that activates a function is knows
as function call.
• Syntax:
–function name(list of arguments);
Note:
• Function call is neccessary to bring the
program control to the function definition.

• If not called, the function statements will


not be executed.
In the 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.
#include <stdio.h>
// 1. Function Declaration (Prototype)
int sum(int a, int b);
int main()
{
int num1 = 10, num2 = 20;
int result;
// 3. Function Call
result = sum(num1, num2); // Call the sum function with num1 and num2 as arguments
printf("The sum of %d and %d is %d.\n", num1, num2, result);
return 0;
}
// 2. Function Definition
int sum(int a, int b)
{
return a + b; // Return the sum of a and b
}
Function arguments & parameters
Parameters in C are classified in two types.
• Actual parameters:
• Parameters that are passed to the function when
function is called.Actual Parameters are the values (or
variables) passed to the function when it is called. They
are the real data the function operates on.
• Formal parameters:
• Parameters those receive values supplied during
function call.Formal Parameters are used in the function
declaration and definition. They are placeholders in the
function's signature.
#include <stdio.h>
int add(int a, int b);// Formal Parameters(Function declaration)
int main()
{
int result = add(10, 20); // '10' and '20' are actual parameters
printf("The sum is: %d\n", result);
return 0;
}
// Function Definition with Formal Parameters
int add(int a, int b)
{
return a + b; // 'a' and 'b' are formal parameters
}
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 specify the return types and
parameters of the function.
• Defining a function: This is where the function’s body is
provided. Here, we specify what the function does, including the
operations to be performed when the function is called.
• 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 back to the caller.
Parameter passing
• In C, there are two common ways to pass parameters to
functions:
1. Call by Value
2. Call by Reference.

• These methods differ in how the arguments are passed to


the function and how changes to them inside the function
affect the calling function.
1. Call by Value:
• Definition: In call by value, the actual value of the
argument is passed to the function. The function works
with a copy of the value, so any changes made to the
parameter inside the function do not affect the original
argument.
• Mechanism: A copy of the argument's value is created,
and that copy is used in the function. The original variable
remains unchanged outside the function.
#include <stdio.h>
// Function to swap two numbers (Call by Value)
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp; OUTPUT:
printf("Inside swap function: a = %d, b = %d\n", a, b);
} Before swap: num1 = 5, num2 = 10
int main() Inside swap function: a = 10, b = 5
{ After swap: num1 = 10, num2 = 5
int num1 = 5, num2 = 10;
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
// Function call (passing num1 and num2 by value)
swap(num1, num2);
// After swap, the original values of num1 and num2 remain unchanged
printf("After swap: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
Call by reference
• In call by reference method address of variable is passed
to the formal arguments of a function.

• Any change in the formal parameter of the function will


effect the value of actual argument.

• same memory location is accessed by the formal and


actual parameter.
//Call by reference
#include <stdio.h>
void swap(int *, int *);
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("Inside swap function: a = %d, b = %d\n", *a, *b);
}
int main()
{
int num1 = 5, num2 = 10;
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swap: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
Types of functions
• Library functions: Library functions are built in
funtions that are stored in header files that comes
with compiler.
• Library functions can be used whenever
required,but their code is not accessible to
programmer or user.
• Library functions are sometimes called Standard
library functions.
• printf(),scanf(),clrscr(),pow(),sqrt() some commonly
used funtions.
Type refers to datatype of quantity that is written
by the function
User defined Functions
• User defined Functions are functions that are defined by
the programmer or user.
• These functions are not provided by the compiler .
• syntax:
return type Function_Name(argument_list)
{
return value;
}
Adventages of User defined functions
#include <stdio.h>
int add(int,int);
int add(int a, int b)
{
return a + b; OUTPUT:
}
int main() Enter any two numbers: 22 44
{ The sum of 22 and 44 is: 66
int num1, num2, sum;
printf("Enter any two numbers: ");
scanf("%d %d", &num1 , &num2);
sum = add(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2,
sum);
return 0; }
Adventages of User defined function
Method of calling function
Function with No argument No return value

• No Arguments: The function does not accept any


parameters when it is called.

• No Return Value: The function does not return any value


to the caller. It simply performs a task or operation without
sending any data back to the function that called it.
Program to calculate area of circle
Function with argument and No return value
• With Arguments: The function takes one or more
parameters when called. These parameters are passed
by the calling function to provide data to the function.

• No Return Value: The function does not return any value


to the calling function. Instead, it performs an operation or
a task, such as modifying variables or printing to the
console.
Function with No argument and return value
• No Arguments: The function does not take any input
parameters when it is called. The function operates
independently of the inputs passed to it.

• Return Value: The function returns a value to the caller.


This value could be of any data type (e.g., int, float, char,
etc.), and it is returned using the return statement.
Function with argument and return value
• With Arguments: The function takes one or more
parameters (arguments) when called. These parameters
are used within the function to perform computations or
operations.

• Return Value: The function returns a result (value) to the


calling function, which could be of any data type (such as
int, float, char, etc.).

You might also like