Functions in C
Functions in C
What is Function in C?
• A set of statements grouped together into a single logical unit is referred to
as Function.
• A function is a self-contained block of code that performs a particular task.
• Example
main()
{
int a,b,c;
c=a+b;
}
• Note: main() is a specially recognized function in C and every C program
must have exactly one main().
Syntax of Functions in C
// Driver code
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
The Formal Parameters are the values determined by the function that accepts values when the function is declared.
• 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.
• 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.
Interaction between Function Blocks – Categories
based on Arguments & Return Values
• Based on the exchange of arguments and return values between
interacting functions, there may be FOUR categories
Category 1 NO NO
Category 2 YES NO
Category 4 NO YES
Example – Display messages from various blocks
• A function call is an expression that passes control and arguments (if any) to a
function.
z
Arguments passed but no return value
Flow of Arguments between Function Blocks - Example
int compute (int *, int *); int compute (int *p1, int *p2)
{
#include <stdio.h> return (* p1 + * p2);
int main() }
{
int a=5,b=6, z; Output
Result is 11
z=compute(&a, &b);
printf(“Result is%d", z);
Pointers p1 & p2 are
return 0; pointing to a and b
} respectively
Handling Non-Integer Function
main() main()
{ {
A 5
int a=5; int a=5;
mod(a); mod(&a);
X <-- 5
printf(“a is %d”, a); +100=105 printf(“a is %d”, a);
} } A 5+100
mod(int x) 105
{ mod(int *x)
x=x+100; {
printf(“x is %d”, x); *x= *x+100;
} }
*x &a
Call by Reference
Call by Value
Categories of C Functions
printf( ) main ( )
scanf() written by the programmer
strlen()
getchar()
--- C allows user to include
--- multiple user defined
functions
How does Multiple Functions Work
Fun ()
main() Arguments
(if any) {
{
---
st-1 Called function
---
St-2
Return values }
St-3: Invoke Fun() (if any)
St-4
St-5
}
Points to Remember
Multiple user defined functions allowed.
Calling function
Any function can invoke any other function and any number of
times
A function can also invoke itself – such are called Recursive
function
A function may send parameters to other function called as
ARGUEMENTS.
A function may perform computation and RETURN back results.
Need for User defined functions
• Enables Modularization of large
complex problems.
• Facilitates top down modular main()
programming.
• Ease of debugging & testing.
• Ease in modifying program. Fun-1 Fun-2 Fun-n
• Repeated use of modules.
• Reduce length of program.
The C Function - Format
• User defined functions must • General Format (function
be given names – The rules definition):
for naming identifier also
holds good for naming
function.
data_type fun_name (argument list)
• General Convention of
{
function declaration is to local variable declaration;
declare a prototype before
main() and at the end of executable statements;
main the function to defined. }
Function invocation using Call-by-Value & Call-by-Reference
T Y prn(); Print T T I M S
I Y prn(); Print I
M Y prn(); Print M Printed in reverse order
S Y prn(); Print S
. N Pop operation
Static Variable Demonstration in Function
•On the contrary, an Auto variable does not retain its value.
1
1
Static Variable Example