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

Functions in C

Functions in C allow programmers to organize code into reusable blocks. There are two types of functions: library functions that are predefined in the standard library, and user-defined functions that are created by the programmer. User-defined functions help make code more modular, reusable, readable, and easier to debug. Functions are declared with a return type, name, parameters, and can optionally return a value. Functions can call other functions, including recursively calling themselves. Arguments are passed into functions either by value, where a copy is made, or by reference, where the function can directly modify the original variable.

Uploaded by

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

Functions in C

Functions in C allow programmers to organize code into reusable blocks. There are two types of functions: library functions that are predefined in the standard library, and user-defined functions that are created by the programmer. User-defined functions help make code more modular, reusable, readable, and easier to debug. Functions are declared with a return type, name, parameters, and can optionally return a value. Functions can call other functions, including recursively calling themselves. Arguments are passed into functions either by value, where a copy is made, or by reference, where the function can directly modify the original variable.

Uploaded by

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

Functions in C

A function is a block of code that performs a particular task.There are


many situations where we might need to write same line of code for
more than once in a program. This may lead to unnecessary repetition
of code, bugs and even becomes boring for the programmer. So, C
language provides an approach in which you can declare and define a
group of statements once in the form of a function and it can be called
and used whenever required.

These functions defined by the user are also know as User-defined


Functions.

C functions can be classified into two categories:

1. Library functions

2. User-defined functions
Library functions are those functions which are already defined in C
library, example printf(),scanf(),strcat() etc. You just need to include
appropriate header files to use these functions. These are already
declared and defined in C libraries.

A User-defined functions on the other hand, are those functions which


are defined by the user at the time of writing program. These functions
are made for code re usability and for saving time and space.

Benefits of Using Functions


1. It provides modularity to your program's structure.

2. It makes your code reusable. You just have to call the function by its
name to use it, wherever required.

3. In case of large programs with thousands of code lines, debugging


and editing becomes easier if you use functions.

4. It makes the program more readable and easy to understand.

Function Declaration
General syntax for function declaration is,

Like any variable or an array, a function must also be declared before its
used. Function declaration informs the compiler about the function
name, parameters is accept, and its return type. The actual body of the
function can be defined separately. It's also called as Function
Prototyping.
Function declaration consists of 4 parts:
 Return type
 function name
 parameter list
 terminating semicolon

Return type:
When a function is declared to perform some sort of calculation or any
operation and is expected to provide with some result at the end, in
such cases, a return statement is added at the end of function body.
Return type specifies the type of value(int, float, char, double) that
function is expected to return to the program which called the function.

Note: In case your function doesn't return any value, the return type
would be void.

Function Name:

Function name is an identifier and it specifies the name of the function.


The function name is any valid C identifier and therefore must follow
the same naming rules like other variables in C language.
Parameter List:

The parameter list declares the type and number of arguments that the
function expects when it is called. Also, the parameters in the
parameter list receives the argument values when the function is called.
They are often referred as formal parameters.

Let's write a simple program with a main() function, and a user defined
function to multiply two numbers, which will be called from
the main() function.
Function definition Syntax
Just like in the example above, the general syntax of function definition
is,

The first line returntype functionName(type1 parameter1, type2


parameter2,...) is known as function header and the statement(s) within
curly braces is called function body.

Note: While defining a function, there is no semicolon(;) after the


parenthesis in the function header, unlike while declaring the function
or calling the function.

Function Body:

The function body contains the declarations and the


statements(algorithm) necessary for performing the required task. The
body is enclosed within curly braces { ... } and consists of three parts.
 local variable declaration(if required).
 function statements to perform the task inside the function.
 a return statement to return the result evaluated by the
 function(if return type is void, then no return statement is
required).

Calling a Function:

When a function is called, control of the program gets transferred to the


function.

In the example above, the statement multiply(i, j); inside the main()
function call.

Parsing Arguments to a Function:

Arguments are the values specified during the function call, for which
the formal parameters are declared while defining the function.
It is possible to have a function with parameters but no return type. It is
not necessary, that if a function accepts parameter(s), it must return a
result too.
While declaring the function, we have declared two
parameters a and b of type int. Therefore, while calling that function,
we need to pass two arguments, else we will get compilation error. And
the two arguments passed should be received in the function definition,
which means that the function header in the function definition should
have the two parameters to hold the argument values. These received
arguments are also known as formal parameters. The name of the
variables while declaring, calling and defining a function can be
different.

Returning a value from a Function:

A function may or may not return a result. But if it does, we must use
the return statement to output the result. return statement also ends
the function execution, hence it must be the last statement of any
function. If you write any statement after the return statement, it won't
be executed.
The datatype of the value returned using the return statement should
be same as the return type mentioned at function declaration and
definition. If any of it mismatches, you will get compilation error.
In the next tutorial, we will learn about the different types of user
defined functions in C language and the concept of Nesting of functions
which is used in recursion.

Types of User-Defined Functions in C:

There can be 4 different types of user-defined functions, they are:


 Function with no arguments and no return value
 Function with no arguments and a return value
 Function with arguments and no return value
 Function with arguments and a return value

Function with no arguments and no return value:

Such functions can either be used to display information or they are


completely dependent on user inputs.

Below is an example of a function, which takes 2 numbers as input from


user, and display which is the greater number.

Function with no argument and return value:


We have modified the above example to make the function
greatNum() return the number which is greater amongst the 2 input
numbers.

Functions with arguments and no return value:

We are using the same function as example again and again, to


demonstrate that to solve a problem there can be many different ways.
This time, we have modified the above example to make the
function greatNum() take two int values as arguments, but it will not be
returning anything.
Functions with arguments and return value:

This is the best type, as this makes the function completely independent
of inputs and outputs, and only the logic is defined inside the function
body.
Nesting of Function

C language also allows nesting of functions i.e to use/call one function


inside another function's body. We must be careful while using nested
functions, because it may lead to infinite nesting.

If function2() also has a call for function1() inside it, then in that case, it
will lead to an infinite nesting. They will keep calling each other and the
program will never terminate.
Lets consider that inside the main() function, function1() is called and its
execution starts, then inside function1(), we have a call for function2(),
so the control of program will go to the function2(). But as function2()
also has a call to function1() in its body, it will call function1(), which will
again call function2(), and this will go on for infinite times, until you
forcefully exit from program execution.

Recursion

Recursion is a special way of nesting functions, where a function calls


itself inside it. We must have certain conditions in the function to break
out of the recursion, otherwise recursion will occur infinite times.

Example: Factorial of a number using Recursion


Types of Function Calls in C

The function does not have any arguments, then to call a function you
can directly use its name. But for functions with arguments, we can call
a function in two different ways, based on how we specify the
arguments, and these two ways are:
1. Call by Value
2. Call by Reference
Call by Value:

Calling a function by value means, we pass the values of the arguments


which are stored or copied into the formal parameters of the function.
Hence, the original values are unchanged only the parameters inside the
function changes.

In this case, the actual variable x is not changed. This is because we are
passing the argument by value, hence a copy of x is passed to the
function, which is updated during function execution, and that copied
value in the function is destroyed when the function ends(goes out of
scope). So the variable x inside the main() function is never changed and
hence, still holds a value of 10.

But we can change this program to let the function modify the
original x variable, by making the function calc() return a value, and
storing that value in x.

Call by Reference:

In call by reference we pass the address(reference) of a variable as


argument to any function. When we pass the address of any variable as
argument, then the function will have access to our variable, as it now
knows where it is stored and hence can easily update its value.
In this case the formal parameter can be taken as a reference or
a pointer(don't worry about pointers, we will soon learn about them), in
both the cases they will change the values of the original variable.

You might also like