[go: up one dir, main page]

0% found this document useful (0 votes)
8 views20 pages

Unit 1 - Functions

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 20

FUNCTION

• C enables 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.
• A function is a self-contained block of codes or sub programs with a set
of statements that perform some specific task when it is called.
• In C, program execution starts from the main() function. Every C
program must contain a main() function.
• The main function may contain any number of statements.
• These statements are executed sequentially in the order which they are
written.
• The main function can in-turn call other functions.
• When main calls a function, it passes the execution control to that
function.
• The function returns control to main when a return statement is
executed or when end of function is reached.
FUNCTION DECLARATION
• Before using a function, the compiler must know about the
number of parameters and the type of parameters that the
function expects to receive and the data type of the value
that will return to the calling function.
• Placing the function prior to its use enables the compiler to
make a check on the arguments used while calling that
function.
• The programmer-defined function must be defined
separately, either ahead of or after main.
• However, in a “top-down” approach, main appears ahead of
the programmer-defined function definition. In such
situations the function declaration will precede the function
definition.
• Thus the compiler is first alerted that the function will be
defined later in the program. Prototype of a function is also
called signature of the function. This is also known as type
FUNCTION DEFINITION
When a function is defined, space is
allocated for that function in the memory. A
function definition comprises two parts:
o Function header
o Function body
return datatype: A function may return a
value. The return_datatype is the data type of
the value the function returns. Some functions
perform the desired operations without
returning a value. In this case, the
return_datatype is the keyword void.
CONT…
Function Body: The function body contains a
collection of statements that define what the
function does. While
return_data_typefunction_name(data_type
variable1, data_type variable2,…) is known as
the function header, the rest of the portion
comprising of program statements within {} is
the function body which contains the code to
perform the specific task.
The function header: The function header is
same as function declaration. The only
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 part
of that function.
• Once the called function is executed, the program
control passes back to the calling function.
Syntax:
Function_name(variable1,
variable2,…)
• When the function declaration is present before the
function call, the compiler can check if the correct
number and type of arguments are used in the function
call and the returned value, if any, is being used
reasonably.
CONT…
• 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.
• If the parameters passed to a function are
more than what it is specified to accept then
the extra arguments will be discarded.
• If the parameters passed to a function are
less than what it is specified to accept then
the unmatched arguments will be initialized
to some garbage value.
FUNCTION TYPES
• There are basically two types of function in
C
• Built-in(Library) Functions: The system
provided these functions and stored in the
library. Therefore it is also called Library
Functions.
e.g. scanf(), printf(), strcpy, strlwr, strcmp,
strlen, strcat etc.
• To use these functions, programmers need to
include the appropriate C header files.
Built-in functions or standard
library functions
• The standard library functions are built-in functions in C
programming. These functions are defined in header
files. For example,
• The printf() is a standard library function to send
formatted output to the screen (display output on the
screen). This function is defined in the stdio.h header
file. Hence, to use the printf() function, we need to
include the stdio.h header file using #include <stdio.h>.
• The sqrt() function calculates the square root of a
number. The function is defined in the math.h header
file.
String Functions
 strlen(string_name) - Returns the length of string name.
 strcpy(destination, source) - Copies the contents of source
string to destination string.
 strcat(first_string, second_string)- Concatenates or joins first
string with second string. The result of the string is stored in
first string.
 strcmp(first_string, second_string)- Compares the first string
with second string. If both strings are same, it returns 0.
 strrev(string)-Returns reverse string.
 strlwr(string)- Returns string characters in lowercase.
 strupr(string)- Returns string characters in uppercase.
Math Functions
• C Programming allows us to perform
mathematical operations through the
functions defined in <math.h> header file.
The <math.h> header file contains various
methods for performing mathematical
operations such as sqrt(), pow(), ceil(),
floor() etc.
• There are various methods in math.h header
file. The commonly used functions of math.h
header file are given below.
• ceil(number)- Rounds up the given number.
User Defined Function
• A function is a block of code that performs a specific
task. C allows programmer to define functions
according to their need. These functions are known as
user-defined functions.
• Based on prototype, user defined function are further
categorized in four categories.
1. Function with no return and no argument
2. Function with no return but arguments
3. Function with return but no argument
4. Function with return and arguments
Function with no return no argument
• Function with no return no argument, neither returns a value
nor accepts any argument.
• This type of function does not communicate with the caller
function. It works as an independent working block.
• Use this type of function when user have some task to do
independent of the caller function.
Syntax :
void function_name()
{
// Function body
}
Function with no return but with arguments

• Function with no return but with arguments, does not


return a value but accepts arguments as input. For this
type of function you must define function return type
as void.
Syntax :
void function_name(type arg1, type arg2, ...){
// Function body}
Function with return but no arguments
• Function with return but no arguments, returns a value
but does not accept any argument. This type of
function communicates with the caller function by
returning value back to the caller.
Function with return and arguments
• Function with return and arguments,
returns a value and may accept arguments.
Since the function accepts input and
returns a result to the caller, hence this
type of functions are most used and best
for modular programming.
Syntax:
return_type function_name(type arg1, type
arg2, ...)
{
RECURSION
• A recursive function is defined as a function that calls
itself to solve a smaller version of its task until a final call
is made which does not require a call to itself.
Syntax:
recursionfunction()
{
recursionfunction();
}
• A recursive function has following two parts
• The base case- it is the case for which the function is
evaluated without recursion.
• Recursive call- it is the central part of a recursive function
and during each call parameter values come closer to the
EXAMPLE
#include<stdio.h>
#include<conio.h>
int fact(int f) {
if (f &lt; = 1) {
printf("Calculated Factorial");
return 1;
}
return f * fact(f - 1);
}
int main(void) {
int f = 12;
clrscr();
printf("The factorial of %d is %d \n", f, fact(f));
getch();
return 0;
Advantages
• Usually simplicity.
• Flexible in data structure like stacks, queues,
linked list and quick sort.
• The length of the program can be reduced.
• It requires few variables which make program
clean
• It is very useful to solve many mathematical
problems
• Avoidance of unnecessary calling of functions.
Disadvantages
• It requires more memory space.
• The recursion function is not efficient in
execution speed and time.
• Some function calls inside recursion are repeated
or duplicated just like Fibonacci.
• It is hard to trace the logic of a recursive function.
• It is also difficult to debug the code containing
recursion.
• The exit point must be explicitly coded.
PASS BY VALUE AND PASS BY REFERENCE
There are two ways through which we can
pass the arguments to the function such as
call by value and call by reference.
Call by value: In the call by value copy of
the actual argument is passed to the formal
argument and the operation is done on
formal argument.
• When the function is called by ‘call by
value’ method, it doesn’t affect content of
the actual argument. Changes made to
formal argument are local to block of called
Example
void main()
{
int a,b;
change(int *,int*); printf(“enter two values:\n”); scanf(“%d
%d”,&a,&b); change(&a,&b);
printf(“after changing two value of a=%d and b=%d\n:”a,b);
}
change(int *a, int *b)
{
int k; k=*a;
*a=*b;
*b= k;
printf(“value in this function a=%d and b=%d\n”,*a,*b);
}

You might also like