Functions allow programmers to break programs into smaller, more manageable units called functions to make programs more modular and easier to write and debug; functions contain elements like a function prototype, parameters, definition, and body; and there are different types of functions like user-defined functions, library functions, and categories of functions based on whether they have arguments or return values.
Automating Google Workspace (GWS) & more with Apps Script
Functions
1. Session – 18 Functions
Session Outcomes
• Why to use functions?
• Elements of user defined functions
• Types of functions
• Categories of functions
2. Why to Use Functions?
• All of the programs that we have studied so far have
consisted of a single function, main().
• If the project is large, and requires you to write code
with more number of lines, it would be difficult for
you to write all alone
• It is also difficult to find errors
Solution: Modular Programming
Breaking a program up into smaller, manageable unit is called
modular programming. These smaller units are called functions
or methods or subroutines ….
3. Examples of functions
main ()
{
int a;
a = sum(3,4);
printf(“%d”,a);
}
int sum (int x, int y)
{
int ans=x+y;
return (ans);
}
main ()
3, 4
sum(int x, int y)
3
x
4
y
7
ans
7
monitor
7
a
7
4. Elements of User defined functions
Elements of User defined functions are
• Function Prototype
• Function Call
• Actual parameters/arguments
• Formal parameters
• Function Definition
• Function Body
A function is a block of code that performs a particular task
when called.
5. Elements of User defined functions
main ()
{
int a;
a = sum(3,4);
printf(“%d”,a);
}
int sum(int,int); Function Prototype
Function Call
3,4 are actual
parameters
int sum (int x, int y)
{
int ans=x+y;
return (ans);
}
Function Definition
x and y are formal
parameters
6. Function Prototype
• It specify the type of value that is to be return
from the function and that is to be passed to the
function.
• It is defined in the beginning before the function
call is made.
• Syntax:
return-type name-of-function(list of arguments);
Example
int sum(int, int);
7. Actual and Formal Parameters
• Actual parameters are written within parenthesis at
the time of function call.
• Formal parameters are written within parenthesis at
the time of function definition.
8. Function Call
• A function can be called by specifying name and list
of parameters enclosed in parenthesis and separated
by comma.
• If there is no parameter empty parenthesis are placed
after function name. Ex: sum();
• If function return a value, function call is written as
assignment statement as:
Example:
a=sum(x,y);
9. Function Definition and Function Body
• It is the independent program module.
• It is written to specify the particular task that is to be
performed by the function.
• The first line of the function is called function
declaration and rest line inside { } is called function
body
10. Return Type
• Return statement will take you out of function
• It is the last statement of the function that return
certain values.
• Function returns a result to the calling statement. The
data type of the value that function returns must be
specified, otherwise void must be specified.
• Function in C can return only one value
Syntax:
return-type (variable-name or constant);
11. Memory Allocation
int sum(int,int);
main ()
{
int a;
Stack Memory
main()
a
a = sum(3,4);
sum( 3, 4)
3
x
int sum (int x, int y)
{
int ans=x+y;
4
y
7
ans
return (ans);
}
7
a
printf(“%d”,a);
}
12. Memory Allocation
• The function data area is an area of memory that is
allocated each time the function is called, and is lost
when the function terminates.
• This data area holds the function's formal parameters,
and local variables created within.
• Variables created inside a function are local to that
function, and can be used only inside that function.
They are not directly accessible from main.
13. Some More Advantages of Functions
• When the same code need to be repeated in more than
one place in a given program
• Then we can put that code in a function and can call
the function as many times as we want in different
places in program where required.
• Why?
– it happens some times that you fix an error in one place
and forget to fix it in others.
– Encapsulating commonly used operations/ code into
functions also promotes the reuse of code from one
program to another.
14. Types of Functions
1. User Defined Functions –written by the programmer
2. Library (Built In) Functions
• They are written in the header files by the C developers.
• To use them appropriate header files should be included.
Header Files Functions Defined
stdio.h printf(), scanf(), getchar(), putchar(),
gets(), puts(), fopen(), fclose()
conio.h clrscr(), getch()
ctype.h toupper(), tolower(), isalpha()
math.h pow(), sqrt(), cos(), log()
stdlib.h rand(), exit()
string.h strlen(), strcpy(), strupr()
15. Categories of Functions
1. Function without arguments and with return type
2. Function with arguments without return type
3. Function without arguments and with return type
4. Function with arguments and with return type
20. Write a program to take radius as an argument to function
area() and calculate area of circle and return its value.
#include<stdio.h>
#include<math.h>
double calArea(double r)
{
return M_PI*r*r;
}
main()
{
double r;
printf("enter radius");
scanf("%lf",&r);
double area=calArea(r);
printf("Area = %lf",area);
}