0% found this document useful (0 votes)
11 views19 pages

Unit 3 - Functions

Chapter 11 discusses the concept of functions in C programming, highlighting their ability to segment code into manageable, independent tasks. It explains the importance of function declarations, definitions, and calls, as well as the differences between passing parameters by value and by reference. The chapter emphasizes that using functions simplifies coding, testing, and program development by allowing for modular design.

Uploaded by

Praghya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Unit 3 - Functions

Chapter 11 discusses the concept of functions in C programming, highlighting their ability to segment code into manageable, independent tasks. It explains the importance of function declarations, definitions, and calls, as well as the differences between passing parameters by value and by reference. The chapter emphasizes that using functions simplifies coding, testing, and program development by allowing for modular design.

Uploaded by

Praghya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CHAPTER 11

FUNCTIONS
Introduction
• C enables its programmers to break up a program into
segments commonly known as functions, each of which can
be written more or less independently of the others.
• Every function in the program is supposed to perform a well
defined task. Therefore, the program code of one function is
completely insulated from that of other functions.
• Every function has a name which acts as an interface to the
outside world in terms of how information is transferred to it
and how results generated by the function are transmitted
back from it. This interface is specified by the function
name.
Introduction (Contd..)
• In the fig, main() calls another function, func1() to perform a
well defined task.
• main() is known as the calling function and func1() is
known as the called function.
• When the compiler encounters a function call, instead of
executing the next statement in the calling function, the
control jumps to the statements that are a part of the called
function.
• After the called function is executed, the control is returned
back to the calling program. main() func1()
{ {
………….. Statement Block;
………….. }
func1();
…………
………..
return 0;
}
Introduction (Contd..)
● It is not necessary that the main() can call only one function, it
can call as many functions as it wants and as many times as it
wants. For example, a function call placed within a for loop,
while loop or do-while loop may call the same function
multiple times until the condition holds true.
● It is not that only the main() can call another functions. Any
function can call any other function. In the fig. one function
calls another, and the other function in turn calls some other
function.
main() func1() func2() func3()
{ { { {
………….. ……….. ……….. ………..
………….. ………… ………. ……….
func1(); func2(); func3(); ………..
………… ……….. ……….. return;
……….. ………. ………. }
return 0; return; return;
} } }
Why Do We Need Functions?
• Dividing the program into separate well defined functions
facilitates each function to be written and tested separately.
This simplifies the process of getting the total program to
work.
• Understanding, coding and testing multiple separate functions
are far easier than doing the same for one huge function.
• If a big program has to be developed without the use of any
function (except main()), then there will be countless lines in
the main() .
• All the libraries in C contain a set of functions that the
programmers are free to use in their programs. These
functions have been prewritten and pre-tested, so the
programmers use them without worrying about their code
details. This speeds up program development.
Terminology of Functions
• A function, f that uses another function g, is known as the
calling function and g is known as the called function.
• The inputs that the function takes are known as arguments.
• When a called function returns some result back to the calling
function, it is said to return that result.
• The calling function may or may not pass parameters to the
called function. If the called function accepts arguments, the
calling function will pass parameters, else not.
• Main() is the function that is called by the operating system
and therefore, it is supposed to return the result of its
processing to the operating system.
Function Declaration
• Function declaration is a declaration statement that
identifies a function with its name, a list of arguments that it
accepts and the type of data it returns.
• The general format for declaring a function that accepts some
arguments and returns some value as result can be given as:
return_data_type function_name(data_type variable1,

data_type variable2,..);
• No function can be declared within the body of another
function.
Example, float avg ( int a, int b);
Function Definition
• Function definition consists of a function header that identifies
the function, followed by the body of the function containing the
executable code for that function.
• When a function defined, space is allocated for that function in
the memory.
• The syntax of a function definition can be given as:
return_data_type function_name(data_type var1, data_type var2,..)
{ ………….
statements
………….
return( variable);
}
• The no. and the order of arguments in the function header must
be same as that given in function declaration statement.
Function Call
• The function call statement invokes the function.
• When a function is invoked the compiler jumps to the called
function to execute the statements that are a part of that
function.
• Once the called function is executed, the program control
passes back to the calling function.
• Function call statement has the following syntax.
function_name(variable1, variable2, …);
Function Call
Points to remember while calling the function:
• Function name and the number and type of arguments in the
function call must be same as that given in the function
declaration and function header of the function definition.
• Names (and not the types) of variables in function declaration,
function call and header of function definition may vary.
• Arguments may be passed in the form of expressions to the
called function. In such a case, arguments are first evaluated
and converted to the type of formal parameter and then the
body of the function gets executed.
• If the return type of the function is not void, then the value
returned by the called function may be assigned to some
variable as given below.
variable_name = function_name(variable1, variable2, …);
Program That Uses Function
#include<stdio.h>
int sum(int a, int b); // FUNCTION DECLARATION
int main()
{
int num1, num2, total = 0;
printf(“\n Enter the first number : “);
scanf(“%d”, &num1);
printf(“\n Enter the second number : “);
scanf(“%d”, &num2);
total = sum(num1, num2); // FUNCTION CALL
printf(“\n Total = %d”, total);
return 0;
}
// FUNCTION DEFNITION
int sum ( int a, int b) // FUNCTION HEADER
{ // FUNCTION BODY
return (a + b);
}
Return Statement
● The return statement is used to terminate the execution of
a function and return control to the calling function.
Programming Tip: It is an error to use a return statement
in a function that has void as its return type.
● A return statement may or may not return a value to the
calling function. Syntax: return <expression> ;
● Expression is placed in between angular brackets because
specifying an expression is optional. The value of
expression, if present, is returned to the calling function.
However, in case expression is omitted, the return value of
the function is undefined.
● Programmer may or may not place the expression within
parentheses. By default, the return type of a function is int.
● For functions that has no return statement, the control
automatically returns to the calling function after the last
statement of the called function is executed.
Passing Parameters to the Function
• There are two ways in which arguments or parameters can be
passed to the called function.
• Call by value in which values of the variables are passed by
the calling function to the called function.
• Call by reference in which address of the variables are passed
by the calling function to the called function.

Passing parameters to
function

Call by
Call by value
reference
Call By Value
● In the Call by Value method, the called function creates
new variables to store the value of the arguments passed to
it.
● Therefore, the called function uses a copy of the actual
arguments to perform its intended task.
● If the called function is supposed to modify the value of the
parameters passed to it, then the change will be reflected
only in the called function.
● In the calling function no change will be made to the value
of the variables.
Call By Value (Contd..)
#include<stdio.h>
void add( int n);
int main()
{
int num = 2;
printf("\n The value of num before calling the function = %d", num);
add(num);
printf("\n The value of num after calling the function = %d", num);
return 0;
}
void add(int n)
{
n = n + 10;
printf("\n The value of num in the called function = %d", n);
}

The output of this program is:


The value of num before calling the function = 2
The value of num in the called function = 12
The value of num after calling the function = 2
Call By Value (Contd..)
The following points are to be noted while passing arguments to a
function using the call-by-value method.

 When arguments are passed by value, the called function creates new
variables of the same data type as the arguments passed to it.
 The values of the arguments passed by the function are copied into
the newly created variables.
 Arguments are called by value when the called function does not need
to modify the values of the original variables in the calling function.
 Values of the variables in the calling function remain unaffected when
the arguments are passed by using call-by-value technique.
Call By Value (Contd..)
 Therefore, call-by-value method of passing arguments to a function
must be used only in two cases:
o When the called function does not need to modify the value of
the actual parameter. It simply uses the value of the parameter to
perform its task.
o When we want the called function should only temporarily
modify the value of the variables and not permanently. So
although the called function may modify the value of the
variables, these variables remain unchanged in the calling
function.
Call By Reference
● When the calling function passes arguments to the called
function using call by value method, the only way to return
the modified value of the argument to the caller is explicitly
using the return statement. The better option when a
function can modify the value of the argument is to pass
arguments using call by reference technique.
● In call by reference, we declare the function parameters as
references rather than normal variables. When this is done
any changes made by the function to the arguments it
received are visible by the calling program.
● To indicate that an argument is passed using call by
reference, an ampersand sign (&) is placed after the type in
the parameter list. This way, changes made to that parameter
in the called function body will then be reflected in its value
in the calling program.
Program Illustrating Call By Reference
Technique
#include<stdio.h>
void add( int *n);
int main()
{
int num = 2;
printf("\n The value of num before calling the function = %d",
num);
add(&num);
printf("\n The value of num after calling the function = %d",
num);
return 0;
}
void add( int *n)
{
*n = *n + 10;
printf("\n The value of num in the called function = %d", *n);
}
The output of this program is:
The value of num before calling the function = 2
The value of num in the called function = 12
The value of num after calling the function = 12

You might also like