MODULE 4-WORKING With Functions
MODULE 4-WORKING With Functions
WITH FUNCTIONS
MODULAR PROGRAMMING
▪The process of identifying the tasks and various sub-tasks involved in the program
design is called modular programming.
▪A module is self contained and intended to perform a single task. It will be
independent. It will be relatively short.
▪There may be large no of program modules and sub-modules in a complex program.
▪Every program has a special module called main module from where the program
execution begins. The main module performs the most fundamental tasks in the
program.
▪When the program is coded, main module becomes the main program and all other
modules may be called sub-programs.
▪A statement in main program calls the sub-module into action. Once the sub-module
has completed the task, execution goes back to the main module.
ADVANTAGES OF MODULAR
PROGRAMMING
▪Program readability is improved.
▪It is easier to design, code and test the program in modules than all at once.
▪Different modules of a complex program can be designed by different programmers.
▪A single module can be used in more than one place in the program thereby reducing
the coding job.
▪Modules can be reused by creating library of such modules.
FUNCTIONS
❑Functions are derived datatypes to perform a particular task
❑Functions break larger computing tasks into smaller ones
❑Every C program has at least one function, which is main(), and all the most trivial
programs can define additional functions
❑ A function will carry out its intended action whenever it is called from some other
portion of the program. Once it complete the actions, control will be returned to the
point at which function was accessed.
ELEMENTS OF FUNCTION
1. Function declaration/function prototype
2.Function definition
3. Function call
Function prototype/Function declaration
•Function declaration may be placed in two places:-
1. Above all functions including main()-global prototype
2. Inside a function definition-local prototype
}
❑The data-type specifies the type of value that function will return through
function name using the return statement.
❑ When function does not return a value, the data type is to be specified as
void.
❑When a return statement is encountered, control is transferred to the calling
portion of the program.
return STATEMENT
❑ return statement- It is used for 2 uses.
❑It can be used for an immediate exit from function it is in.
❑ Second, it can be used to return a value.
return(expression);
❑The value of the expression is returned to the calling portion of the program.
❑A return statement can be written even without an expression.
❑If the expression is omitted, return causes control to go back to the calling portion
without transfer of any information.
FUNCTION EXAMPLE
/* function returning the max between two numbers */
int max(int num1, int num2)
{ int result; /* local variable declaration */
Let x=125
1st iteration
digit=125%10=5
rev=0*10+5=5
X=125/10=12
2nd iteration
digit =12%10=2
rev=5*10+2=52
X=12/10=1
3rd iteration
digit =1%10=1
rev=52*10+1=521
X=1/10=0
SCOPE OF VARIABLES
A scope in any programming is a region of the program where a defined variable can
have its existence and beyond that variable it cannot be accessed. There are three
places where variables can be declared in C programming language −
Inside a function or a block which is called local variables.
Outside of all functions which is called global variables.
In the definition of function parameters which are called formal parameters.
Let us understand what are local and global variables, and formal parameters.
LOCAL VARIABLES
❑Variables that are declared inside a function or block are called local variables. They
can be used only by statements that are inside that function or block of code. Local
variables are not known to functions outside their own. The following example
shows how local variables are used. Here all the variables a, b, and c are local to
main() function.
#include <stdio.h>
int main ()
{ /* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10; b = 20; c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
GLOBAL VARIABLES
❑ Global variables are defined outside a function, usually on top of the program.
Global variables hold their values throughout the lifetime of your program and they
can be accessed inside any of the functions defined for the program.
❑ A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration. The following
program show how global variables are used in a program
#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{ /* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
A program can have same name for local and global variables but the value of local
variable inside a function will take preference. Here is an example −
#include <stdio.h>
/* global variable declaration */
int g = 20;
int main ()
{ /* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of g = 10
FORMAL PARAMETERS
❑ Formal parameters, are treated as local variables with-in a function and they take
precedence over global variables. Following is an example −
#include <stdio.h>
/* global variable declaration */
int a = 20;
int main ()
{ /* local variable declaration in main function */
int a = 10; int b = 20; int c = 0;
printf ("value of a in main() = %d\n", a);
c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}
/* function to add two integers */
int sum(int a, int b)
{ printf ("value of a in sum() = %d\n", a);
printf ("value of b in sum() = %d\n", b);
return a + b;
}
When the above code is compiled and executed, it produces the following result −
value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30
TYPES OF USER DEFINED
FUNCTIONS
1. No arguments passed and no return value
Eg-
void sum()
{
int a,b, value;
printf(“Enter the value of a & b”);
scanf(“%d%d”, &a,&b);
value =a+b;
printf(“Sum =%d”,value);
}
2. No arguments passed but return value
int sum()
{
int a, b, value;
scanf(“%d%d”,&a,&b);
value=a+b;
return (value);
}
3.Arguments passed but no return value
Eg-
void sum(inta,intb)
{
int value;
scanf(“%d%d”, &a,&b);
value =a+b;
printf(“sum=%d”,value);
}
4. Function with arguments passed and return value
Eg-
int sum(inta, intb)
{
int value;
scanf(“%d%d”, &a,&b);
value =a+b;
return (value);
}
PASSIING VALUES TO
FUNCTIONS
Sr.No. Call Type & Description
1 Call by valueThis method copies the actual value of an argument into the
formal parameter of the function. In this case, changes made to the
parameter inside the function have no effect on the argument.
2 Call by reference This method copies the address of an argument into the
formal parameter. Inside the function, the address is used to access the
actual argument used in the call. This means that changes made to the
parameter affect the argument.
By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the
arguments used to call the function.
CALL BY VALUE METHOD
CALL BY REFERENCE METHOD
WAP TO FIND THE FACTORIAL OF
A NUMBER
#include<stdio.h> long factorial (int x)
l
int main() {
int i, fact =1;
{
for(i=1;i<=n;i++)
int n,i; {
printf(“Enter the number \n”); fact = fact *i;
scanf(“%d”,&n); }
return fact;
printf(“factorial of %d is %d”
}
, n, factorial(n));
RECURSION
❑ Recursion is the process of repeating items in a self-similar way. In programming
languages, if a program allows you to call a function inside the same function, then it
is called a recursive call of the function.
Eg: factorial of a number
n!=1x2x3x…x(n-1)xn
n!=nx(n-1)!
❑ The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function, otherwise it
will go into an infinite loop.
RECURSION EXAMPLE-
FACTORIAL
PASSING ARRAYS TO
FUNCTIONS
❑ Like scalar variables, arrays can also be passed to functions. It is possible to pass entire
array as an arguments to function. Here name of array and size of array are given as
arguments.
Eg: average (num,n) --- function calling where num is array name and n is size of array
float average(int num[],int n) ----- function declaration.
❑ The formal argument requires a set of empty square brackets to indicate that num is an
array.
❑ When an array is passed to a function, the values of array elements are not copied to the
function. Name of array is interpreted as the address of the memory location containing
the first array element. This address is assigned to the corresponding formal argument
when a function is called. The arguments passed in this manner is called call by reference.
WRITE A C PROGRAM TO FIND OUT LARGEST
AMONG A LIST OF NUMBERS USING FUNCTIONS.