2. WAProgram ????? output
HELLO I AM A FUNCTION NAMED HELLO
WELCOME I AM A FUNCTION NAMED WELCOME
HELLO I AM A FUNCTION NAMED HELLO
HELLO I AM A FUNCTION NAMED HELLO
WELCOME I AM A FUNCTION NAMED WELCOME
HELLO I AM A FUNCTION NAMED HELLO
3. Need/Advantages of function
• Modular Programming
• Makes C program readable
• Enhance application software process
• Program development becomes easy
• Reusability
• Module once created can be used any no of time
4. Definition – Function
• encapsulates a subset of a program
• To hide details
• To be invoked from multiple places
• To share with others
• A fragment of code
• accepts zero or more argument values
• produces a result value, and has zero or more side effects.
5. Functions
• Types of Functions in C
• Function definition
• Function prototypes & Header files
• Pre- and post-conditions
• Scope and storage class
• Implementation of functions
• Recursive functions
12. Definitions
• Argument:–
• an expression passed when a function is called
• int a;
• a=pow(2,3);
• 2 and 3 are the arguments
Function call of pow()
• Returns type = int
• Argument = 2 integers
13. Examples of function definition
• int pow(int a,int b)
{
int ans,i;
ans=1;
for(i=1;i<=b;i++)
ans=ans*a;
}
// pow function
14. Function Prototypes
• a function must be used separate from where it is defined
–
• before its definition in the same C program
• Therefore, we need some way to declare a function
separate from defining its body.
• Called a Function Prototype
15. Function Prototypes (continued)
• Definition:– a Function Prototype in C is a language
construct of the form:–
return-type function-name (parameter declarations) ;
17. Pointers
• A pointer is a reference to another variable in a program
• Stores the memory location
18. Pointer Variable Definition
• Basic syntax:
datatype *ptrName
• Can declare pointers to any data type
• Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
• Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints *
19. Pointer Variable Initialization - Address Operator
• The address (&) operator points the memory location of
the variable in memory
Syntax: &variablename
Examples:
int a=10;
int *b;
b=&a;
Pointer variable ‘b’
holding the
address of variable
‘a’
20. Dereferencing Pointers - Indirection (*) Operator
• Is to get the value of the variable it refers to
• Syntax: *PointerVariable
Example:
int age = 19;
int *P = &V;
printf(“%d”,*P);
/* Prints 19 */
21. Go through …..
* and & are inverses
Cancel each other out
*&myVar == myVar
and
&*yPtr == yPtr
22. Knowledge Check
Assume address of A as 2002 and B
as 4005
int A = 3;
int B = 4;
int *P = &A;
int *Q = &B;
printf(“%d %dn”,A,B);
printf(“%d %dn”,*P,*Q);
printf(“%p %pn”,P,Q);
23. Pointers to Pointers
• A pointer can also be made to point to a pointer variable
Example:
int V = 101;
int *P = &V; /* P points to int V */
int **Q = &P; /* Q points to int pointer P */
printf(“%d %d %dn”,V,*P,**Q); /* prints 101 3 times */
24. Pointer Arithmetic
• Increment/decrement pointer (++ or --)
• Add an integer to/from a pointer( + or += )
• Pointers may be subtracted from each other (- or -=)
• Pointer arithmetic is performed on an array
25. Pointer Arithmetic on Arrays
• 5 element int array on a machine using 4 byte ints
• vPtr points to first element v[ 0 ], which is at location 3000
• vPtr = 3000
• vPtr += 2; sets vPtr to 3008
• vPtr points to v[ 2 ]
pointer variable
vPtr
v[0] v[1] v[2] v[4]v[3]
3000 3004 3008 3012 3016
location
26. Characters and Strings with Pointers
• String assignment
• Character array:
char color[] = "blue";
• Creates 5 element char array, color, (last element is '0')
• variable of type char *
char *colorPtr = "blue";
• Creates a pointer to string “blue”, colorPtr, and stores it somewhere in
memory
27. Arrays of Pointers
• Arrays can contain pointers
• Commonly used to store an array of strings
char *suit[ 4 ] = {"Hearts", "Diamonds",
"Clubs", "Spades" };
• Each element of suit is a pointer to a char * (a
string)
suit[3]
suit[2]
suit[1]
suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’0’
’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’0’
’C’ ’l’ ’u’ ’b’ ’s’ ’0’
’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’0’