C/Function/Function Parameters

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A function to increase your salary

#include <stdio.h>
long *IncomePlus(long* pValue);     /* function Prototype */
int main() {
   long your_value = 30L;
   long *pold_value = &your_value;
   long *pnew_value = NULL;
   pnew_value = IncomePlus( pold_value );
   printf("\n Old value = $%d", *pold_value);
   printf("   New value = $%d\n", *pnew_value);
}

long *IncomePlus(long *pValue)
{
   *pValue += 10L;
   return pValue;
}


Calculating an average using variable argument lists

#include <stdio.h>
#include <stdarg.h>
double average(double v1 , double v2,...);
int main()
{
  printf("\n Average = %lf", average(3.5, 4.5, 0.0));
  printf("\n Average = %lf", average(1.0, 2.0));
  printf("\n Average = %lf\n", average(0.0,1.2,1.5));
}
double average( double v1, double v2,...)
{
  va_list parg;
  double sum = v1+v2;
  double value = 0;
  int count = 2;
  va_start(parg,v2);
  while((value = va_arg(parg, double)) != 0.0)
  {
    sum += value;
    printf("\n in averge = %.2lf", value);
    count++;
  }
  va_end(parg);                /* End variable argument process      */
  return sum/count;
}


Char pointer as the function parameter

  
#include <stdio.h>
void f(char *p);
int main(void)
{
  f("this is a test");
  return 0;
}
void f(char *p)
{
  while(*p) { /* loop as long as p does not point to the
                 null which is the string terminator*/
    printf("%c", *p);
    p++;       /* go to next character */
  }
  printf("\n");
}


Computes the area of three triangles

#include <stdio.h>
float triangle(float width, float height)
{
    float area;
    area = width * height / 2.0;
    return (area);
}
int main()
{
    printf("Triangle #1 %f\n", triangle(1.3, 8.3));
    printf("Triangle #2 %f\n", triangle(4.8, 9.8));
    printf("Triangle #3 %f\n", triangle(1.2, 2.0));
    return (0);
}


Define constant function parameter

#include <stdio.h>
void print_str(const char *p);
int main(void)
{
  char str[80];
  printf("Enter a string: ");
  gets(str);
  print_str(str);
  return 0;
}
void print_str(const char *p)
{
  while(*p) 
      putchar(*p++);
}


Define int pointer parameter for a function

#include <stdio.h>
void f(int *k);
main () {
  int i;
  i = 0;
    
    printf (" i before call %d \n", i);
  
  f(&i); 
    
    printf (" i after call %d \n", i);
}
void  f(int *k) 
{
  *k = *k + 10; 
}


Demonstrate the use of pointers and parameter passing

#include <stdio.h>
void inc_count(int *count_ptr)
{
  ++(*count_ptr);
}
int main()
{
    int count = 0;
    while (count < 10){
        inc_count(&count);
    }    
    return (0);
}


Length of the function parameters

#include <stdio.h>
#include <stdarg.h>
void print_message(char *format, ...);
int main(void)
{
  print_message("Cannot open file %s.", "test");
  return 0;
}
void print_message(char *format, ...)
{
  va_list ptr; /* get an arg ptr */
  /* initialize ptr to point to the first argument after the
     format string
  */
  va_start(ptr, format);
  /* print out message */
  vprintf(format, ptr);
  va_end(ptr);
}


Pass Array into a function

  
#include <stdio.h>
void printarr(int a[]) {
    int i;
    for(i = 0;i<5;i++) {
        printf(" %d\n",a[i]);
    }
}
main() {
    int a[5];
    int i;
    
    for(i = 0;i<5;i++) {
        a[i]=i; 
    }
    printarr(a);  
}


Pass array value into function: by array, by empty array and by pointer

  
#include <stdio.h>
void f1(int num[5]), f2(int num[]), f3(int *num);
int main(void)
{
  int count[5] = {1, 2, 3, 4, 5};
  f1(count);
  f2(count);
  f3(count);
  return 0;
}
/* parameter specified as array */
void f1(int num[5])
{
  int i;
  for( i = 0; i < 5; i++) 
      printf("%d ", num[ i ]);
}
/* parameter specified as unsized array */
void f2(int num[])
{
  int i;
  for( i = 0; i < 5; i++) 
      printf("%d ", num[ i ]);
}
/* parameter specified as pointer */
void f3(int *num)
{
  int i;
  for(i = 0; i < 5; i++) 
      printf("%d ", num[ i ]);
}


Pass array with different dimension into function

#include <stdio.h>
void print_onedim(int a[]);
void print_twodim(int a[][4]);
void print_threedim(int a[][3][4]);
main() {
    int cnt=0;
    int a[2][3][4];
    int i;
    int j;
    int k;
    for(i = 0;i < 2; i++){
        for(j = 0;j < 3; j++){
            for(k = 0;k < 4; k++) {
                a[i][j][k] = cnt;
                cnt++;
            }
        }
    }
    print_onedim(a[1][1]);
    print_twodim(a[1]);
    print_threedim(a);
}
void print_onedim(int a[]) {
    int i;
    for(i = 0; i < 4 ; i++)
        printf("%d ", a[i]);
}
void print_twodim(int a[][4]) {
    int j;
    for(j = 0;j < 3; j++)
        print_onedim(a[j]);
    printf("\n");
}
void print_threedim(int a[][3][4]) {
    int j;
    printf("Each two dimension matrix\n");
    for(j = 0; j < 2 ; j++)
        print_twodim( a [ j ] );
}


Pass char pointer into function

#include <stdio.h>
void print_vertical(char *str);  /* prototype */
int main(int argc, char *argv[])
{
  if(argc > 1) print_vertical(argv[1]);
  return 0;
}
void print_vertical(char *str)
{
  while(*str)
    printf("%c\n", *str++);
}


Pass double value into function

  
#include <stdio.h>
double feet_to_meter(double f);
int main(void)
{
  double feet;
  printf("Enter feet: ");
  scanf("%lf", &feet);
  printf("Meters: %f", feet_to_meter(feet));
  return 0;
}
double feet_to_meter(double f)
{
  return f / 3.28;
}


Passing parameter by pointer

  
#include <stdio.h>
void prompt(char *msg, int *num);
int main(void)
{
  int i;
  prompt("Enter a num: ", &i);
  printf("Your number is: %d", i);
  return 0;
}
void prompt(char *msg, int *num)
{
  printf(msg);
  scanf("%d", num);
}


Passing the data type addess into the function

#include <stdio.h>
void swap(int *x, int *y);
int main(void)
{
  int i, j;
  i = 10;
  j = 20;
  printf("i and j before swapping: %d %d\n", i, j);
  swap(&i, &j); /* pass the addresses of i and j */
  printf("i and j after swapping: %d %d\n", i, j);
  return 0;
}
void swap(int *x, int *y)
{
  int temp;
  temp = *x;  /* save the value at address x */
  *x = *y;    /* put y into x */
  *y = temp;  /* put x into y */
}


Pass reference

#include <stdio.h>
int change(int* pnumber);   /* Function prototype               */
void main()
{
   int number = 20;         
   int* pnumber = &number;  
   int result = 0;          
   result = change(pnumber);
   printf("\nIn main, result = %d\tnumber = %d", result, number);
}
int change(int* pnumber)
{
   *pnumber *= 2;
   printf("\n In function change, *pnumber = %d\n", *pnumber );
   return *pnumber;
}


Pass reference of an int value into function

#include <stdio.h>
void f(int *k) {
  *k = *k + 10; 
}
main ( ) {
  int i;
  i = 0;
  
    printf ("The value of i before call %d \n", i);
  
  f(&i); 
    
    printf ("The value of i after call %d \n", i);
}


Pass return value through function parameter

#include <stdio.h>
#include <stdlib.h>

void mean(int a,int b, int *ptr_to_answer) {
    *ptr_to_answer = (a + b)/2;
}
int main() {
    int i, j;
    int answer;
    i = 6;
    j = 9;
    mean(i,j, &answer); /* passing a pointer to "answer" */
    
    printf(" The mean of %d and %d = %d\n", i, j, answer);
}


Pass value

#include <stdio.h>
int change(int number);
void main()
{
   int number = 20;    
   int result = 12;     
   result = change(number);
   printf("\n In main, result = %d \t number = %d", result, number);
}

int change(int number) {
   number = 2 * number;
   printf("\n In function change, number = %d\n", number);
   return number;
}


Return value though parameter

#include <stdio.h>
#include <stdlib.h>
void mean(int a, int b, int return_val ) {
   return_val = (a + b) / 2;
   printf("return_val in mean in %d\n",return_val);
}
int main() {
    int i, j;
    int answer;
    i = 7;
    j = 9;
    mean(i, j, answer);
    printf("The mean of %d and %d = %d\n", i, j, answer);
}


Use function with pointer parameters

void f(int *k) {
  *k = *k + 10; 
}

int main () {
  int i = 0;
    
    printf (" i before call %d \n", i);
  f(&i); 
    printf (" i after call %d \n", i);
}