Lesson 07 - Functions
Lesson 07 - Functions
Functions
Formal Arguments
Actual Arguments
It transfers the control from the function back to the calling program
immediately.
Local Variables
Declared inside a function
Created upon entry into a block and destroyed upon exit
from the block
Formal Parameters
Declared in the definition of function as parameters
Act like any local variable inside a function
Global Variables
Declared outside all functions
Holds value throughout the execution of the program
automatic
external
static
register
Call by value
Call by reference
When arguments are passed to the called function, the values are
passed through temporary variables
The arguments are said to be passed by value when the value of the
variable are passed to the called function and any alteration on this
value has no effect on the original value of the passed variable
/* Pass-by-Value example */
#include <stdio.h>
int swap (int a, int b);
int main ()
{
int x = 19, y = 5;
printf("Before swapping: x=%d, y = %d\n",x,y);
swap(x, y);
printf("After swapping: x=%d, y = %d",x,y);
return 0;
}
int swap (int a, int b)
Output
{
int temp;
temp = a;
a = b;
b = temp;
}
Definition
getstr(char *ptr_str, int *ptr_int);
Call
getstr(pstr, &var);
/* Pass-by-Reference example */
#include <stdio.h>
int swap (int *a, int *b);
int main ()
{
int x = 19, y = 5;
printf("Before swapping: x=%d, y = %d\n",x,y);
swap(&x, &y);
printf("After swapping: x=%d, y = %d",x,y);
return 0;
} Output
int swap (int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
main() palindrome()
{ {
. .
. .
getstr();
palindrome reverse();
(); cmp();
. .
. .
} }