Learnem, Computer education over Internet - Since the year 2000
Home Ebook E-learning Courses Free E-books Login Discussion Boards
Lesson 7 : Function Arguments

Function Arguments

In previous lesson you saw how we could send a single value to a function using a function argument.  In this lesson we will send multiple parameters to a function. We will also learn about call by value and call by reference “call types”.

Multiple Parameters

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: example7-1.c

#include<stdio.h>

int min(int a,int b);

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

 system("pause");
 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. We learned earlier that function prototype is a copy of function header with the difference that prototype ends with semicolon ‘;’ but function header does not.

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: example7-2.c

#include<stdio.h>

void test(int a);
main()
{
 int m;
 m=2;

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

 system("pause");
 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 we have modified value of incoming parameter inside test() function. What do you think?

Program output result 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 the function and it does not send variable itself. Actually it places value of variable m in a memory location called stack and then function retrieves the value without having access to main variable itself. This is why we call this method of calling “call by value”.

Call by reference

There is another method of sending variables being called “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: example7-3.c

#include<stdio.h>
void test(int *ptr);
main()
{
int m;
m=2;

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

system("pause");
return 0;
}

void test(int *ptr)
{
printf("\nModifying the value inside memory address %ld",&m);
*ptr=5;
}

To be able to modify the value of a variable used as an argument in a function, function needs to know memory address of the variable (where exactly the variable is situated in memory).

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 starting 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. Now we understand the reason why we add ‘&’ sign before variable names in scanf variables.

  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
Modifying the value inside memory address 2293620
M is 5

So you see this time we have changed value of an argument variable inside the called function (by modifying the value inside the memory location of our main variable). The memory address will change each time you run the program or on different compilers and computers.

A useful example, Bubble Sort

In this section we will write a program which sorts an array of numbers using a famous bubble sort algorithm. This algorithm uses a “swap” function which swaps values stored in two memory locations with each other.

Bubble sort compares each two cells of the array and if they are not in correct order, it swaps them. If we need the array to be sorted in ascending order, each cell should be bigger or equal to previous cell and the first cell should be smaller than any other cell. If we perform these compares and swaps for “n-1” times on the entire array, our array will be completely sorted.

Example 7-4 (Bubble sort): example7-4.c

#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]);

system("pause");
return 0;
}

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

The output is:

Array before sort:

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

Array after sort:

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

End Note

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

In next lessons we will learn more about arrays, pointers, strings, files, structures, memory management etc. 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.

3. Describe (in terms of memory locations etc.) how the swap function in our last example works.


 

Return to Index Page


Links
Discussion Boards
Paid Ebooks
Free Ebooks
Testimonials
Faq
Contact
About us
Course Pages
Web Design
C Programming
Free Tutorials
Web Design
PHP Web Prog.
C Prog.
ASP Web Prog.
Free Ebooks
Web Design Ebook
C Prog. Ebook
Free Support
Support for e-books and free courses are offered through CourseFarm website. In order to use the CourseFarm website, You need to create a free member account on it (see main page of the website).

Web Design in 7 Days Course Page

Programming in C in 7 Days Course Page

PHP Web Programming in 7 Days Course Page

Support on CourseFarm is provided by authors of the e-books themselves. You may also contact them on the website for your possible questions.
Home | Educational E-books | Online Courses | Free Books | Discussion Boards
Testimonial | Contact us | FAQ | About us
Society50 Asian Social Network | Science and Tech Blogs | Asia Weblog | CupidB Free MatchMaker

© 2000-2010 Learnem.com, All Rights Reserved. Last Update April, 2011.