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

Module 3

The document provides an overview of functions in C programming, detailing their syntax, declaration, definition, and calling mechanisms. It explains the importance of functions for modularity and code reusability, as well as the different types of function return values and parameters. Additionally, it covers the steps involved in declaring, defining, calling, executing, and returning values from functions.

Uploaded by

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

Module 3

The document provides an overview of functions in C programming, detailing their syntax, declaration, definition, and calling mechanisms. It explains the importance of functions for modularity and code reusability, as well as the different types of function return values and parameters. Additionally, it covers the steps involved in declaring, defining, calling, executing, and returning values from functions.

Uploaded by

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

‘C’ Programming

By Asst. Prof. Sudeshna Baliarsingh


Information & Technology Dept.
KJSIT, Mumbai
Functions

● Introduction to functions

● Function prototype, Function definition

● Accessing a function and parameter passing:


Call by Value and Call by reference

● Recursive functions

● Storages Classes: Auto, extern, Static and Register


● Introduction to functions
● 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 i.e promotes 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/prototype
● Function Definition
● 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.
Return Type: Usually, the function calculates a value, and return it
back to the main() program. This value is said to be the return value.
We need to declare what type of variable will be returned from the
function. This can be int, double, char, string, and even void if the
function does not return anything.

Function Name: Just like variables, every function has a name. This
has to be included in the function declaration. Note that the function
name should be unique, just like variable names.

Parameters: Sometimes the function needs specific inputs to execute


the code inside. These inputs are passed to the function to be used in
the form of function parameters. A function can have more than one
Function Declarations
The syntax for declaring a function in C is :
return_type function_name (parameter_data_type
p1, parameter_data_type p2, ..);

Here, return_type refers to the type of variable returned by the function,


function_name refers to the name of the function, and p1 and p2 are the
names of the variables being passed as a parameter.

Example
int sum(int a, int b);
int sum(int , int);
Function Declarations

Note: A function in C must always be declared globally before calling it.


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. Here, return_type refers to the type of variable returned by the function,
function_name refers to the name of the function, and p1 and p2 are the names of
the variables being passed as a parameter.
The below example serves as both a function definition and a declaration.
return_type function_name (parameter_data_type
p1, parameter_data_type p2, ..);
{
// body of the function
}
Function Definition
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.

Note: Function call is necessary to bring the program control to


the function definition. If not called, the function statements will
not be executed.
Calling a Function

Now that we have declared and defined the function, we can execute the
code written inside it anytime by just one line in C.

function_name(p1, p2);
Here p1 and p2 are the parameters or that were previously mentioned
during the declaration of the function. By just this one line, all the code
inside the function that we defined above will be executed.

Note: While calling a function, the same number of parameters should be


given as input as in the declaration. Also, these parameters should have the
same data type as in the declaration.
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.
Output :
Enter two numbers : 4
5
sum=9
Here we are running the
code without function
declaration .

It will run and ask you to enter


number but it will also give
you warning
“Implicit declaration of
function” and compiler will
assume the return type is
integer.

It will run as we are not


returning anything.
Warning:
Implicit declaration of function

Error : Conflicting types of “sum”

So corrected program is below:


If we are passing arguments
in defination it May or may not
give error as we are asking to Error: Too many arguments
enter no. (line3) to the function( line 2 and 3)
Example of C Function Output
Sum is: 40

As we noticed, we have not


used explicit function
declaration. We simply defined
and called the function.

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.
Area of Circle Program in C using Function

Area is 78.550000
Area of Circle Program in C using Function

Enter the Radius of the


Circle: 5
The Area of the Circle is:
78.500000
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);


Function Arguments and Return values
Function Arguments (also known as Function Parameters) are the data that is
passed to a function.
Example:

int function_name(int var1, int var2);

There are four different aspects of function calls, based on whether a function
accepts arguments and/or returns a value. A function may accept no arguments
and return no value, accept no arguments and return a value, accept arguments
and return no value, or accept arguments and return a value namely:

● function with arguments and without return value


● function with arguments and with return value
● function without arguments and without return value
● function without 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.

You might also like