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

Function & Macro

The document provides an overview of functions in C programming, including their definitions, types (library and user-defined), and benefits such as code reusability and manageability. It explains function prototypes, calling methods (call by value and call by reference), recursion, and preprocessor directives. Additionally, it discusses macro expansion, file inclusion, conditional compilation, and miscellaneous directives used in C programming.

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

Function & Macro

The document provides an overview of functions in C programming, including their definitions, types (library and user-defined), and benefits such as code reusability and manageability. It explains function prototypes, calling methods (call by value and call by reference), recursion, and preprocessor directives. Additionally, it discusses macro expansion, file inclusion, conditional compilation, and miscellaneous directives used in C programming.

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Function

A function in C language is a block of code


that performs a specific task. It has a name
and it is reusable i.e. it can be executed
from as many different parts in a C Program
as required. It also optionally returns a value
to the calling program.

Function

Library function User define function


Benefits of
functions
Divide and conquer

• Each piece more manageable than the


original program

Software reusability
• Use existing functions as building blocks for
new programs
• Abstraction - hide internal details (library
functions)

Avoid code repetition
Math Library


Functions
Math library functions

perform common mathematical calculations


#include <math.h>


Format for calling functions


FunctionName( argument );

• If multiple arguments, use comma-separated list


printf( "%.2f", sqrt( 900.0 ) );

• Calls function sqrt, which returns the square root of its argument

• All math functions return data type double


Arguments may be constants, variables, or expressions
Function
Definitions

Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}


Function-name: any valid identifier


Return-value-type: data type of the result (default int)

• void – indicates that the function returns nothing


Parameter-list: comma separated list, declares parameters

• A type must be listed explicitly for each parameter unless,


the parameter is of type int
return-value-type function-name( parameter-list )
{
declarations and statements
}


Declarations and statements: function body (block)

• Variables can be declared inside blocks (can be nested)

• Functions can not be defined inside other functions


Returning control

• If nothing returned

return;

or, until reaches right brace

• If something returned

return expression;
Function Prototypes

Function prototype


Function name


Parameters – what the function takes in


Return type – data type function returns (default int)


Prototype only needed if function definition comes after use in
program


The function with the prototype
int maximum( int, int, int );

• Takes in 3 ints

• Returns an int
Calling Functions: Call by Value and
Call by Reference

Call by value

-Pass the ‘value’ of variables to the called function


Copy of argument passed to function


Changes in function do not effect original


Use when function does not need to modify argument

• Avoids accidental changes


Call by reference

-Call by reference means passing the address of a variable where

the actual value is stored.


Passes original argument


Changes in function effect original
Example of Call by value
#include<stdio.h>

Void swap(int x,int y);

Void main()

int a=10,b=20;

swap(a,b);

printf(“\na=%d b=%d”,a,b);

}
Example of Call by value
continues..
Void swap(int x,int y) Out Put

{
int t; X=20 y=10
t=x; a=10 b=20
x=y;
y=t;
printf(“\n x=%d y=
%d”,x,y);
}
Example of Call by
#include<stdio.h>
Reference
Void swap(int *,int *);

Void main()

int a=10,b=20;

swap(&a,&b);

printf(“\na=%d b=%d”,a,b);

}
Example of Call by Reference
continues..
Void swap(int *x,int *y)
{
int t; OutPut

t=*x; a=20 b=10


*x=*y;
*y=t;
}
Recursion

Recursive functions

Functions that call themselves
A function is called “recursive” if
a statement within body of that
function calls the same function.
Recursion
Example..
Example :
Factiorial of 5 = 5x4x3x2x1
int factorial (int i)
{
int f;
if(i==1)
return 1;
else
f = i* factorial (i-1);
return f;
}
Example Using Recursion: The
Fibonacci Series

Fibonacci series: 0, 1, 1, 2, 3, 5, 8...


Each number is the sum of the previous two


Can be solved recursively:

• fib( n ) = fib( n - 1 ) + fib( n – 2 )


Code for the fibaonacci function

long fibonacci( long n )

if (n == 0 || n == 1) // base case

return n;

else

return fibonacci( n - 1) +fibonacci( n – 2 );

}

Set of recursive calls to function
fibonacci
f( 3 )

return f( 2 ) + f( 1 )

return f( 1 ) + f( 0 ) return 1

return 1 return 0
Preprocessor

Preprocessor is a program that processes our source
program before it is passed to the compiler.

Preprocessor works on the source code & creates Expanded
source code.

If the source code is stored in a file PR1.C,then expanded
source code gets stored in a file PR1.I,that sends to the
compiler for compilation.

The preprocessor offers several feature called preprocessor
directives.

Each of these preprocessor directives begins with #
symbol.
Preprocessor directives are four type

a)Macro expansion (#define)


b)File inclusion (#include)
c)Conditional compilation (#ifdef)
d)Miscellaneous directive (#undef)
Macro Expansion
#include<stdio.h>

#define UPPER 25 # define UPPER 25 is called


macro definition.
void main() During preprocessing, the
preprocessor replaces every
{ occurrence of UPPER in the
program with 25.
int i;

for(i=1;i<UPPER;i++)

printf(“\n%d”,i);

}
Macros with
#define AREA(x) (3.14*x*x)

void main() Arguments


{

float r1=2.5,a;

a=AREA(r1);

printf(“\nArea of circle=%f”,a);

The x in the macro template AREA(x) is an argument that matches


the x in the macro expansion (3.14 *x*x).
Macros Vs
Function
Macros Function
Macros are faster than Function are slower tan
function but taking more macros but taking less
memory space . memory space
Macros placed in the source Passing arguments to a
code before compilation. function & getting back the
returned value does take
time.
If we use a macro hundred If a function is called from
times in a program, the macro hundred different places in
expansion goes into our the program, it would take the
source code at hundred same amount of space in the
different place thus increase program.
program size.
File Inclusion

This directive causes one file to be
included in another. These are:
#include "filename"
or #include <filename>

#include "filename”
File Inclusion
continues..
If the file name is enclosed in quotes, the preprocessor
searches in the same directory as the file being
compiled for the file to be included. This method is
normally used to include programmer defined headers.

#include <filename>

If the file name is enclosed in brackets - used for


standard library headers - the search is performed in an
implementation dependent manner, normally through
predestinated directories.
Conditional
Compilation
If we want compiler skip over part of a source code
by

inserting the preprocessing commands #ifdef and

#endif.

# ifdef macroname

statement 1;

statement 1;

statement 1;

#endif
Miscellaneous
Directive
a) #undef derective:- It is used to undefine a macro that has been earlier
#defined.

#undef PENTIUM

The statement will cause the definition of PENTIUM to be removed from the
system.

b) #pragma directive:- This is a special purpose directive that can be used


to turn on or off certain features.Some of the pragmas are

•#pragma startup and #pragma exit: These directives allow to specify


functions that are called upon program start up (before main())or exit (just
before the program terminates).

•#pragma warn: This directive tells the compiler whether or not we want to
suppress a specific warning.

You might also like