0% found this document useful (0 votes)
64 views

Functions in C

Functions in C can be defined and called in various ways: 1. Functions are defined using a function prototype specifying the return type and parameters. 2. Function definitions can precede or follow the main() function as long as a prototype is declared beforehand. 3. Functions can call other functions and pass arguments either by value or by reference. Passing by reference allows the called function to modify the original arguments. 4. One-dimensional and two-dimensional arrays can be passed to functions either by value using individual elements or by reference using pointers or array names.

Uploaded by

Satadru Das
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Functions in C

Functions in C can be defined and called in various ways: 1. Functions are defined using a function prototype specifying the return type and parameters. 2. Function definitions can precede or follow the main() function as long as a prototype is declared beforehand. 3. Functions can call other functions and pass arguments either by value or by reference. Passing by reference allows the called function to modify the original arguments. 4. One-dimensional and two-dimensional arrays can be passed to functions either by value using individual elements or by reference using pointers or array names.

Uploaded by

Satadru Das
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Functions in C

Defining a function
data- type name( type1 arg1, type2 arg2, . . ., type n arg n)
Example 2
Example 3
Function Prototypes
• In the examples seen before, the function
definition precedes the main() function.
• However, we can define the function after the
main function too ……….. PROVIDED!!!!!
WE “DECLARE” THE FUNCTION BEFORE
MAIN… WHY??
Function Calls
• A function can be called within another
function or the main() function.
• The main() function is the first function the
compiler will start execution from.

• Practice.. Game of Craps!


Passing Arguments!
• Two ways
– Pass by Value
– Pass by Reference
• When you pass by value the original variable is
not modified!
• When you pass by reference the original is
affected!
Passing Arguments! – Pass by value
Pass by Reference
Example: Swapping
• As you are aware, swapping 2 values is a common
routine – for instance, when sorting
• Here, we look at the wrong way and the right way to
swap two values in a function

void swap(int a, int b) void swap(int *a, int *b)


{ {
int temp = a; int temp = *a;
a = b; *a = *b;
b = temp; *b = temp;
} }

The wrong way to swap The right way to swap

When called with swap(x, y); When called with swap(&x, &y); where x and y
x and y will remain the same are int values, then a points to x and b points
to y, so the values pointed to are swapped,
not the pointers themselves
Passing Arrays – call by value
#include <stdio.h>
void disp( char ch)
{
printf("%c ", ch);
}
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
for (int x=0; x<10; x++)
{
/* I’m passing each element one by one using subscript*/
disp (arr[x]);
}
 
return 0;
}
Passing array to function using call by reference

#include <stdio.h>
void disp( int *num)
{
printf("%d ", *num);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
for (int i=0; i<10; i++)
{
/* Passing addresses of array elements*/

disp (&arr[i]);
}
return 0;
}
How to pass an entire array to a function as an argument?

#include <stdio.h>
void myfuncn( int *var1, int var2)
{ /* The pointer var1 is pointing to the first element of * the
array and the var2 is the size of the array. In the * loop we
are incrementing pointer so that it points to * the next
element of the array on each increment. * */
for(int x=0; x<var2; x++)
{
printf("Value of var_arr[%d] is: %d \n", x,*var1);
/*increment pointer for next element fetch*/
var1++;
}
}
int main()
{
int var_arr[] = {11, 22, 33, 44, 55, 66, 77};
myfuncn(var_arr, 7);
return 0;
}
Passing 2D Array to Functions
#include <stdio.h>
const int n = 3;
 
void print(int arr[3][3], int m)
{
    int i, j;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    print(arr, 3);
    return 0;
}
First Dimension is Optional
#include <stdio.h>
const int n = 3;
 
void print(int arr[][n], int m)
{
    int i, j;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        printf("%d ", arr[i][j]);
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    print(arr, 3);
    return 0;
}
Recursive Functions

You might also like