Function & Macro
Function & Macro
Function
•
Functions
Math library functions
•
#include <math.h>
•
Format for calling functions
•
FunctionName( argument );
•
printf( "%.2f", sqrt( 900.0 ) );
• Calls function sqrt, which returns the square root of its argument
•
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)
•
Parameter-list: comma separated list, declares parameters
•
Declarations and statements: function body (block)
•
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
•
Copy of argument passed to function
•
Changes in function do not effect original
•
Use when function does not need to modify argument
•
Call by reference
•
Passes original argument
•
Changes in function effect original
Example of Call by value
#include<stdio.h>
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
•
Each number is the sum of the previous two
•
Can be solved recursively:
•
Code for the fibaonacci function
if (n == 0 || n == 1) // base case
return n;
else
}
•
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
for(i=1;i<UPPER;i++)
printf(“\n%d”,i);
}
Macros with
#define AREA(x) (3.14*x*x)
float r1=2.5,a;
a=AREA(r1);
printf(“\nArea of circle=%f”,a);
#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>
#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.
•#pragma warn: This directive tells the compiler whether or not we want to
suppress a specific warning.