C语言及程序设计提高例程-5 函数的参数

贺老师教学链接  C语言及程序设计提高 本课讲解

定义无参函数

#include <stdio.h>
void printstars( )
{
    printf("*******************\n");
}
void printmessage(void)
{
    printf("Hello, world.\n");
}
int main()
{
    printstars();
    printmessage();
    printstars();
    return 0;
}

定义有参函数

#include <stdio.h>
void printchs(int m, char ch)
{
    int j;
    for (j=1; j<=m; ++j)
        putchar(ch);
}
int main()
{
    int i;
    for(i=1; i<=6; ++i)
    {
        printchs(6-i,' ');
        printchs(2*i-1,'*') ;
        printf("\n");
    }
    return 0;
}

函数应用实例:求f1(x)=3x^3+2x^2-1

#include <stdio.h>
float  f1(float x)
{
    float y;
    y=3*x*x*x+2*x*x-1;
    return y;
}
int main()
{
    float x, y;
    scanf("%f", &x);
    y=f1(x);
    printf("x = %.3f, y = %.3f\n", x, y);
    return 0;
}

形式参数和实际参数

#include <stdio.h>
int gcd(int n1, int n2)
{
    int r;
    while(n2!=0)
    {
        r=n1%n2;
        n1=n2;
        n2=r;
    }
    return n1;
}
int main()
{
    int num1,num2;
    int iGcd, iLcm;
    scanf("%d%d",&num1,&num2);
    iGcd=gcd(num1, num2);
    printf("the gcd is: %d\n",iGcd);
    return 0;
}

实参变量对形参变量进行“值传递”

#include <stdio.h>
void fun(int x, int y)
{
   x=x*10;
   y=y+x;
   printf("%d\t%d\n", x, y);
}

int main()
{
   int a=2, b=3;
   fun(a+b,a*b);
   printf("%d\t%d\n", a, b);
   return 0;
}

形参和实参对应的存储空间

#include <stdio.h>
void fun(int x, int y)
{
   x=x*10;
   y=y+x;
   printf("%d\t%d\n", x, y);
}

int main()
{
   int a=2, b=3;
   fun(a+b,a*b);
   printf("%d\t%d\n", a, b);
   return 0;
}

实参单元与形参单元是不同的单元

#include <stdio.h>
void fun(int a, int b)
{
   a=a*10;
   b=b+a;
   printf("%d\t%d\n", a, b);
}

int main()
{
   int a=2, b=3;
   fun(a, b);
   printf("%d\t%d\n", a, b);
   return 0;
}
时间: 2024-08-01 15:17:30

C语言及程序设计提高例程-5 函数的参数的相关文章

C语言及程序设计提高例程-8 函数的声明、定义和调用

贺老师教学链接  C语言及程序设计提高 本课讲解 调用的条件--被调用的函数已存在 #include <stdio.h> #include <math.h> float max(float x, float y); int main () { float a,b,c,s; scanf("%f %f", &a, &b); s=sqrt(a); printf("sqrt is %.2f\n", s); c=max(a+b, a*b

C语言及程序设计提高例程-9 函数的嵌套调用

贺老师教学链接  C语言及程序设计提高 本课讲解 函数的嵌套调用:函数内调用函数 #include <stdio.h> int gcd(int n1, int n2); int lcm(int n1, int n2); int main() { int num1,num2; int iGcd, iLcm; //最大公约数和最小公倍数 printf("please input two numbers:"); scanf("%d%d",&num1,&

C语言及程序设计提高例程-4 函数的返回值

贺老师教学链接  C语言及程序设计提高 本课讲解 函数返回 #include <stdio.h> int max(int x,int y); int main() { int a, b, c; scanf(%d %d", &a, &b); c = max(a, b); printf("max=%d", c); return 0; } int max(int x, int y) { return(x>y?x:y); }

C语言及程序设计提高例程-25 指针作函数参数

贺老师教学链接  C语言及程序设计提高 本课讲解 例:冒泡排序函数的新写法 #include <stdio.h> void bubblesort(int*, int); int main( ) { int i,a[10]= {3,5,9,1,3,6,-9,-7,10,12}; bubblesort(a,10); for(i=0; i<10; i++) printf("%d ", a[i]); return 0; } void bubblesort(int *p, in

C语言及程序设计提高例程-36 多维数组作函数参数

贺老师教学链接  C语言及程序设计提高 本课讲解 用多维数组名作函数参数 #include <stdio.h> int max_value(int array[][4]); int main( ) { int a[3][4]= {{11,32,45,67},{22,44,66,88},{15,72,43,37}}; printf("max value is %d\n", max_value(a)); return 0; } int max_value(int array[]

C语言及程序设计提高例程-27 编写查找和排序函数

贺老师教学链接  C语言及程序设计提高 本课讲解 用函数实现二分查找 #include <stdio.h> int binary_search(int arr[], int n, int k); #define SIZE 10 int main( ) { int d[SIZE] = {1, 3, 9, 12, 32, 41, 45, 62, 75, 77}; int low, high,mid,key,index=-1; printf("Input a key you want to

C语言及程序设计提高例程-24 数组名作为函数参数

贺老师教学链接  C语言及程序设计提高 本课讲解 用数组元素作函数实参 #include <stdio.h> int gcd(int m,int n) { int r; while(r=m%n) { m=n; n=r; } return n; } int main() { int i; int a[8]= {26,1007,956,705,574,371,416,517}; int b[8]= {994,631,772,201,262,763,1000,781}; int c[8]; for(

C语言及程序设计提高例程-7 返回指针的函数

贺老师教学链接  C语言及程序设计提高 本课讲解 返回指针的函数 #include <stdio.h> int *max(int *x, int *y) { int *t; if(*x > *y) t = x; else t = y; return t; } int main() { int a, b, *p; scanf("%d %d", &a, &b); p = max(&a, &b); printf("max = %d

C语言及程序设计提高例程-2 模块化程序设计及C语言中的函数

贺老师教学链接  C语言及程序设计提高 本课讲解 我们写过这样的程序 #include <stdio.h> int main() { int iChioce; do { printf("* 1. 吃饭\n"); printf("* 2. 睡觉\n"); printf("* 3. 打豆豆\n"); printf("* 0. 退出\n"); printf("* 请选择(0-3):"); scanf(