Custom functions in C Language

Source: Internet
Author: User

Custom functions in C Language

In C language, you can use system functions or your own functions, that is, user-defined functions.

There are four types of custom functions

First:

Function Declaration

void sayH();

Function implementation

Void sayH () {printf ("hello ");}

Type 2: parameters without return values

Function declaration:

void pxsbx(int c,int k);

Function implementation:

void pxsbxx(int c,int k){    int i,j;    for (i=0; i<c; i++) {        for (j=0; j<=i; j++) {            printf(" ");        }        for (j=0; j<k; j++) {            if (j==0||j==k-1||i==0||i==c-1) {                printf("*");            }else{                  printf(" ");            }              }        printf("\n");    }}

Function call

Int c, k; printf ("Enter length:"); scanf ("% d", & c); printf ("Enter width :"); scanf ("% d", & k); pxsbxx (c, k );

Running result

3. Parameters and return values

Statement:

int add1(int num1,int num2,int num3);

Implementation

int add1(int num1,int num2,int num3){    int sum=num1+num2+num3;    return sum;}

Call

 int num1,num2,num3,sum;    scanf("%d%d%d",&num1,&num2,&num3);    sum=add1(num1, num2,num3);    printf("%d",sum);

Effect

Type 4: parameters without return values

Statement

void add(int num1,int num2);

Implementation

void add(int num1,int num2){    int sum=num1+num2;    printf("%d\n",sum);}

2. recursive functions (two simple recursive calls)

1, factorial

Function Declaration

int MyFun(int num);

Function implementation

Int MyFun (int num) {// common function // int sum1 = 1; // for (int I = 1; I <= num; I ++) {// sum1 * = I; //} // return sum1; // recursively call if (num = 1 | num = 0) {return 1 ;} else {return num * MyFun (num-1 );}}

Call

Printf ("sum = % d \ n", MyFun (6 ));

Running Effect

2. the Fibonacci series

Function Declaration

int fbsnq(int num);

Function implementation

Int fbsnq (int num) {// recursive if (num = 1 | num = 2) {return 1;} return fbsnq (num-1) + fbsnq (num-2 ); // normal // int n1 = 1, n2 = 1, temp = 0; // if (num = 1 | num = 2) {// return 1; //} // int I = 2; // for (; I <num; I ++) {// temp = n1 + n2; // n1 = n2; // n2 = temp; //} // return temp ;}

Function call

    for (int i=1; i<=30; i++) {        if ((i-1)%5==0) {            printf("\n");        }            printf("%10d",  fbsnq(i));    }    printf("\n\n");  

Running result

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.