"Learn C programming!"

7-1 Function Arguments

   

7-1 Function Arguments

In previous lesson you saw how we could send a single value to a function using a function argument.

In fact you can use more than one argument in a function. Example 7-1 will show you how you can do this.

Example 7-1:

#include<stdio.h>

int min(int a,int b);
main()
{
int m;
m=min(3,6);
printf("Minimum is %d",m);

return 0;
}

int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}

As you see you can add your variables to arguments list easily. Function prototype is a copy of function header. The only difference is that prototype ends with semicolon ‘;’ but function header does not.

-----------------------------------------------------------------------------

7-2 Call by value

C programming language function calls use call by value method. Let’s see an example to understand this subject
better.

Example 7-2:

#include<stdio.h>

void test(int a);

main()
{
int m;
m=2;

printf("\nM is %d",m);
test(m);
printf("\nM is %d",m);

return 0;
}

void test(int a)
{
a=5;
}

In main() function, we have declared a variable m. We have assigned value ‘2’ to m. We want to see if function call is able to change value of m as you may expect because we have modified its value inside test() function. What do you think?


Program output results is:

M is 2
M is 2

So you see function call has not changed the value of argument. This s is because function-calling method only sends value of variable m to function and it does not send variable itself. Actually it places value of variable m in a memory called stack and then function retrieves the value without having access to variable itself.

This is why we call this method of calling “call by value”.

-----------------------------------------------------------------------------

7-3 Call by reference

There is another method of sending variables that we can call it “Call by reference”. This second method enables
function to modify value of argument variables used in function call.

We will first see an example and then we will describe it.

Example 7-3:

#include<stdio.h>
void test(int *ptr);

main()
{
int m;
m=2;

printf("\nM is %d",m);
test(&m);
printf("\nM is %d",m);

return 0;
}

void test(int *ptr)
{
*ptr=5;
}


To be able to modify the value of a variable that is used as an argument in a function, function needs memory address of the variable.

In C programming language ‘&’ operator gives the address at which the variable is stored. For example if ‘m’ is a
variable of type ‘int’ then ‘&m will give us the start memory address of our variable.

We call this resulting address ‘a pointer’.

ptr=&m;

In above command ptr variable will contain memory address of variable m. This method is used in some of standard functions in C. For example scanf function uses this method to be able to receive values from console keyboard and put it in a variable. In fact it places received value in memory location of the variable used in function.

scanf("%d",&a);

Now that we have memory address of variable we must use an operator that enables us to assign a value or access to value stored in that address.

As we told we can find address of variable ‘a’ using ‘&’ operator.

ptr=&a;

Now we can find value stored in variable ‘a’ using ‘*’ operator:

val=*ptr; /* finding the value ptr points to */

We can even modify the value inside the address:

*ptr=5;


Let’s look at our example again. We have passed pointer (memory address) to function. Now function is able to modify value stored inside variable.

If you run the program, you will get these results:

M is 2
M is 5

So you see this time we have changed value of an argument variable inside called function.

-----------------------------------------------------------------------------

7-4 A useful example, Bubble Sort

In this section we will write a program that sorts an array using famous bubble sort algorithm.

We have written a function “swap” that swaps values stored in two memory locations. Then we use this function to perform sort.

Bubble sort compares each two cell of array and if they are out of sort, swaps them. If we perform these compares and swaps for n-1 times on all array cells then our array will be completely sorted.


Example 7- Bubble sort:

#include<stdio.h>

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

main()
{
int ar[5],i,j,n;

ar[0]=7;ar[1]=3;ar[2]=9;ar[3]=2;ar[4]=11;

printf("Array before sort:\n\n");
for(i=0;i<5;i++)
printf("ar[%d]=%d\n",i,ar[i]);

n=5; /*numberof items in sort array*/

for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
{
if(ar[j]>ar[j+1])
swap(&ar[j],&ar[j+1]);
}

printf("Array after sort:\n\n");
for(i=0;i<5;i++)
printf("ar[%d]=%d\n",i,ar[i]);


return 0;
}


void swap(int *a,int *b)
{
int temp;

temp=*a;
*a=*b;
*b=temp;
}

-----------------------------------------------------------------------------

7-5 End

We have learned fundamentals of programming in C in these 7 first lessons but there are a lot of things we must learn.

But it’s enough for now!

-----------------------------------------------------------------------------

Exercises:

 

1- Write a function with two arguments. First argument is “ch” from character type and second variable is “repeat” from int type. When you pass a character and a repeat number (int) it prints character for “repeat” times on console.

2- Write a program that reverses the order of members of an array. Use addresses and pointers to displace values in array.


============================================================


BreBru.Com Extra Information Technology HTML