0% found this document useful (0 votes)
39 views12 pages

Understanding Functions in C Programming

Chapter 10 discusses the importance of functions in C programming, highlighting two types: library functions and user-defined functions. It covers the definition, declaration, and calling of functions, as well as the concept of recursion, which allows functions to call themselves. The chapter also provides examples of different types of functions based on arguments and return values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

Understanding Functions in C Programming

Chapter 10 discusses the importance of functions in C programming, highlighting two types: library functions and user-defined functions. It covers the definition, declaration, and calling of functions, as well as the concept of recursion, which allows functions to call themselves. The chapter also provides examples of different types of functions based on arguments and return values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter: 10

FUNCTIONS
C language has more importance on functions. Every C program is a combination of
one or more functions.
C language has 2 type of functions:
1. Library functions
2. User defined functions
Every C program starts with user defined function main(). When a new program gets
started, the main() function has to be defined. The main() calls another functions.
The library functions are predefined set of functions. The user is unaware of the
working of these functions. The user has the privilege of using these functions, but
cannot modify these functions Example: printf(), scanf().
Need /Advantage of Functions:
1. Any program can be coded only with main() function, but it makes the program
larger and complex, hence the task of debugging, testing and maintaining becomes
difficult.
2. Any program can be divided into functional parts, and each part can be
independently coded and combined later so, the maintenance task becomes easier.
3. When some calculations has to be repeated several times through out the program,
one way to achieve the task is to repeat the program statements whenever needed, the
other method is to design a function, that can be called and used whenever required.
So this methodology saves time and space.
4. While using functions, it ensures the following:
• facilitates top-down modular programming.
• The length of the program is reduced
• Testing, debugging and maintenance becomes easy
• A function can be used by other programs
Definition of Function:
• A function can be called a self-contained block. Once a function is designed it
can be called a black-box, because the inner details of the working of function
are unknown to the rest of the program.
• The function may take some data from the main function and it may or may
not return the value.
• Any function can call any other function. A function can call itself.
• A called function can also call other functions. A function can be called any
number of times.
• The function can be placed in any order. A called function can be placed either
before or after the calling function.
• The usual way is to place all the called functions at the end.

User defined functions:


Users can create their own functions, to perform any task, and these functions are
termed as user-defined functions.
There are 3 concepts for the user-defined functions.
1. Function Declaration.
2. Function Definition.
3. Function Call.

Function Definition:
• This part consists of the code of the function, this describes what the function
is doing and what are the inputs and the output.
• The Function definition has 2 parts:-
1. function header
2. function body.
• The syntax of function definition is
return-type function-name(type argument1, type2 argument2...)
{
Local variable declaration;
statement1;
statement2;
statement N;
return(expression);
}
• after the function header, the function body is enclosed within curly braces.
• The return type is the type of values, that will be returned after the function is
executed. This is optional and if it is not specified it is assumed to be int.
• The function may return one value or no value. If a function does not return
any value, then void has to be specified.
• Function name is the name of the function, it is a valid C identifier. The
function-name is followed by the type and name of the arguments. These are
referred to as formal arguments and accepts the values.
• If there are no arguments, then the parenthesis is left empty or void is
specified.
• The function body consists of declaration of local variables and these variables
can be used only within the function.
• There can be any number of valid C statements within the function body.
• The return statement is optional if the function does not return any value.

Example: To add 2 Numbers using functions


#include<stdio.h>
int sum(int x, int y);
main()
{
int a, b, s;
printf(“enter the values for a and b”);
scanf(“%d%d”, &a,&b);
s = sum(a,b);
printf(“the result is %d”,s);
}
int sum(int x, int y)
{
int s;
s= x+y;
return s;
}

Function Call:
A function is called by writing its name followed by the list of arguments
within the parentheses.
• The syntax is
function-name(argument1, argument2,...argument N);
argument1, argument2, argument N are actual arguments
• main() is the calling function.
• We can call the function even there is no actual arguments, the function call
should have empty parentheses.
• When a function is called, the control passes to the called function(it gets
executed) and then the control is transferred to the statement following the
function call in the calling function.
• A function call cannot occur in the left hand of the assignment statement.

Function Declaration:
• The calling function should know about the called function if the called
function is defined before the calling function.
• Generally the function main is defined at the top & all other functions are
placed after it.
• The function declaration is also called function prototyping.
• The function has to be declared and it informs the compiler regarding -
1. Name of the function
2. Number and type of arguments received by the function.
3. Type of value returned by the function.
• The syntax of the function declaration is
return-type function-name(type argument1, type2 argument2...);
this is similar to the function header but a semicolon is present.
The argument is also optional, so it can also be written as
return-type function-name(type1, type2 ..);
if there is no return type then void can be specified.
Void function-name(void);

Return statement:
• The return statement is used in a function to return a value to the calling
function.
• It may be use for immediate exit from the called function without returning a
value.
• The syntax for return statement is
return;
return expression;
Example:
return (a+b);
Function Arguments
The calling function sends the data/ value to the called function, and these are termed
as arguments / parameters.
The arguments are classified into 2 types:
• Actual arguments (fn declaration, fn call)
• Formal arguments (fn defn)
Actual arguments:
• The arguments that are mentioned in the function declaration are called actual
arguments.
• These values are actually sent to the called function.
• The actual arguments may be variable, constants, expressions or any function
call.
Formal Arguments:
• The arguments that are mentioned in the function definition are called formal
or dummy arguments.
• These arguments are used to hold the values that are sent by the calling
function.
• The formal arguments are created when the function call is started and are
destroyed when the function is ended.
• The formal arguments are declared inside the parenthesis and the local
variables are declared at the beginning of the functioning block.
• The formal arguments are initialized when the values are received from the
actual arguments, while the local variable within the function block has to be
assigned the values.
• The type and number of actual arguments in the function call should match
with that of the formal arguments in the function definition.
Example-1
#include<stdio.h>
void fn(int, int);
main()
{
int a=2, b = 3;
fn(a,b);
fn(10,20);
fn(a+b, b-a);
}
fn(int a,int b)
{
printf(“a = %d, b=%d”, a, b);
return;
}
Example-2
main()
{
int a=2, b = 3;
printf(“%d”,mul(a,b));
printf(“%d”,mul(10,20));
printf(“%d”,mul(a+b, b-a));
printf(“%d”,mul(a, sum(a,b));
}

mul(int x, int y)
{
int r;
r= x * y;
return r;
}

sum(int x, int y)
{
return ( x + y);
}
Types of function:
Functions are classified into 4 categories based on the arguments and its return value:
1. Function with no arguments and no return value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and return value

1. Function with no arguments and no return value


Only a function call is in the main function. The main does not send any data or the
called function does not have a return statement.
Example-1
#include<stdio.h>
void sum(void);
main()
{
sum();
}
void sum(void)
{
int a, b, s;
printf(“enter the 2 values”);
scanf(“%d %d”,&a &b);
s= a + b;
printf(“the result is %d”,s);
}

2. Function with no arguments and a return value


Only a function call is in the main function. The main does not send any data but the
called function have a return statement, it may send a value to the calling function.
#include<stdio.h>
int sum(void);
main()
{
int s;
s = sum();
printf(“the result is %d”,s);
}
int sum(void)
{
int a, b, result;
printf(“enter the 2 values”);
scanf(“%d %d”,&a &b);
result = a + b;
return result;
}

3. Function with arguments and no return value


Function call is in the main function. The main send data to the called function, but
the function does not return a value to the calling function.
Example:
#include<stdio.h>
void sum(int, int);
main()
{
int a, b;
printf(“enter the 2 values”);
scanf(“%d %d”,&a &b);
sum(a, b);
}
void sum(int x, int y)
{
int z;
z= x + y;
printf(“the result is %d”,z);
}

4. Function with arguments and return value


Function call is in the main function. The main send data to the called function and
the function returns a value to the calling function.
Example:
#include<stdio.h>
int sum(int, int);
main()
{
int a, b, s;
printf(“enter the 2 values”);
scanf(“%d %d”,&a &b);
s=sum(a, b);
printf(“the result is %d”,s);
}

int sum(int x, int y)


{
int z;
z= x + y;
return z;
}
Recursion

• Recursion is defined as the function calling itself repeatedly.


• Recursion is used to make the complicated algorithms work in a simple form.
• In recursion, the actual problem is divided into smaller problems, that are
similar to the original problem. So these smaller problems are solved and their
solutions are applied to get the final problem.
• C supports recursive methodology.
• In the recursive function, the calling function and the called function are same.
So, whenever recursion is used, there should be a terminating condition to
stop the recursion, otherwise it will keep on calling itself and will never stop.
• So before writing the recursive function for a problem, we should ensure the
following:
1. The solution of the problem can be defined for similar type of smaller
problems.
2. There should be a terminating condition to stop recursion.
• Recursion executes in 2 phases:
1. Winding phase
2. Unwinding phase
• In the winding phase the function keeps on calling itself. The winding phase is
stopped, when the terminating condition occurs.
• Now the second phase starts(unwinding phase) and the called function returns
the value in the reverse order.
• At each time the function is called, there is only one copy of that function in
memory, because each function call is different from another, because the
argument is different at each time.

Program for factorial of a number using recursion


#include<stdio.h>
int fact (int n);
main()
{
int num;
Printf(“enter a number”);
scanf(“%d”, &num);
printf(the factorial of %d is %d”, num, fact(num));
}

int fact(int n)
{
if(n==0)
return 1;
else
return (n* fact(n-1));
}

You might also like