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

MODULE 4-WORKING With Functions

Uploaded by

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

MODULE 4-WORKING With Functions

Uploaded by

Joel George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

MODULE 4—WORKING

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

•Function declaration or function prototype consists of 4 parts:-


✔Function return type
✔Function name
✔Parameter list
✔Terminating colon
FUNCTION DECLARATION
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
Syntax
Returndata_type function_name(parameter list);
Eg- int sum (int a, int b); or int sum(int, int);
void display(void);
Note:-
Parameters must be separated by comma
Use of parameter names is optional
Parameter list must match in number and order with parameter in function definition
Parameter name must not be same in function declaration and definition
The return type must be viod if function returns no value
FORWARD DECLARATION
❑There are some situations when the function call has to be made before function
definition.
❑In such cases, there must be a function declaration in the calling portion of the
program or prior to that.
❑This is called forward declaration.
❑The declaration informs the compiler that a function will be accessed before it is
defined.
FUNCTION DEFINITION
❑Function definition consists of three main components
❑A heading or first line which has function return type, function name,
followed by optional list of parameters enclosed in parentheses.
❑A compound statement which comprises the body of the function.
Returndata_type function_name( list of parameters)
{
local variable declaration;
executable statement;
return statement;

}
❑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 */

if (num1 > num2)


result = num1;
else
result = num2;
return (result);
}
CALLING A FUNCTION
❑ When a function is defined in a program, it can be accessed from anywhere
within that program.
❑ Accessing a function is also known as function call.
❑ A function call consists of the function name followed by a list of arguments
enclosed in parenthesis separated by commas.
❑ The function call can appear as a simple expression statement or as an
operand within an expression.
❑ When a program calls a function, the program control is transferred to the
called function. A called function performs a defined task and when its
return statement is executed, it returns the program control back to the
main program.
For eg- sum(a,b); or x=sum(a,b);
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{ /* local variable definition */
int a = 100;
int b = 200;
int ret;
ret = max(a, b); /* calling a function to get max value */
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{ /* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
OUTPUT:
We have kept max() along with main() and compiled the source code. While running
the final executable, it would produce the following result −

Max value is : 200


FUNCTION ARGUMENTS OR
PARAMETERS
Actual parameter/argument:
❑ The arguments that appear on a function call are known as actual arguments or
actual parameters.
❑Actual parameters may be variables, constants or expressions
Formal parameter/argument:
❑The arguments that appear in function declaration and function definition are formal
arguments.
❑When a function call is executed, control is transferred to the function and the values
of actual arguments are copied to the respective formal arguments
Note- Formal and actual parameters must match in number, order and datatype.
Illustration of how one function calls other functions.
Q. Write a C program to read an integer number and reverse it using a function.

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.

You might also like