C - Passing a value to a function

Introduction

You can pass an argument to a function.

Arguments are specified in the function's parentheses.

An example is the puts() function, which accepts text as an argument, as in

puts("hi."); 

Arguments can be variables or constant values, and multiple arguments are separated by commas.

The number and type of values must be specified when the function is defined and for its prototype as well.

The following code shows how to pass a Value to a Function.

Both the prototype and the display() function's definition state that the argument must be an int.

The variable count is used as the int argument, which then serves as the variable's name inside the function.

The display() function is called in the midst of the while loop.

It's called using the value variable.

The variable you pass to a function doesn't have to match the variable name used inside the function.

Only the variable type must match.

The display() function displays a row of asterisks.

The length of the row (in characters) is determined by the value sent to the function.

Demo

#include <stdio.h> 

void display(int count); 

int main() //from w  ww  .  ja v a  2 s.  c  o  m
{ 
   int value; 

   value = 2; 

   while(value<=64) 
   { 
       display(value); 
       printf("Value is %d\n",value); 
       value = value * 2; 
   } 
   return(0); 
} 

void display(int count) 
{ 
   int x; 

   for(x=0;x<count;x=x+1) 
       putchar('*'); 
   putchar('\n'); 
}

Result

Related Topic