C language----functions

Source: Internet
Author: User

First, the definition:

A collection of code fragments with specific functionality, we become functions.

Second, return value:

1. The return value of the function is called the value of the function and can only be returned to the keynote function through the return statement;
2, a function can have only one return value;
3, the function even if there is no return value can also use return to end the execution of the function, return to the keynote function;
4. The statement after return is no longer executed.

Third, formal parameters, actual parameters

1. Formal parameters
Appears in the definition of the function, with a false name to represent the value to be provided when the function is called.
2.
An expression that appears in a function call.

Note: When a function is called, the contents of the formal participant copy argument are a way to convey information to the function.

Iv. function declarations

Strict functions usually consist of three parts: function declaration, function definition, and function tuning.

V. Recursive invocation of functions

1,//For the function of recursive call must do a good analysis, the derivation of the recursive expression, n * Multiplefun (n-1), and in order to prevent the loop call itself causes the program into a dead loop, we must find the conditions of the end of the call, such as factorial is the condition of 1 o'clock stop loop call

2, the so-called recursive call function to implement the internal loop call itself until the condition is not satisfied.

Six, example code:
Enter a month and date to calculate the day ordinal of the year?#import <Foundation/Foundation.h>intSumvalue (intn);intDayOfYear (intYearintMonthintDay);intMainintARGC, const char * argv[]) {//    intn =0;//    printf("Please enter a number:");//scanf"%d", &n);//    printf("1 ~ %d and is %d\ n", N, Sumvalue (n));//    printf("Please enter date and year information:");intYear =0, month =0, day =0; scanf"%d/%d/%d", &year, &month, &day);printf(%d%d months  %d is  the day of the year.  \ n ", year, month, Day, DayOfYear (year, month, day));return 0;}intSumvalue (intN) {intSum for(inti =1; I <= N; i++) sum + = i;returnsum;}intDayOfYear (intYearintMonthintDay) {intDays[] = { to, -, to, -, to, -, to, to, -, to, -, to};//If the entered month is greater than A, or the date is greater than toIndicates an input error, directly ends the input, and returns0Daysif(Month > A|| Day > to)    {return 0; }if(Year% -==0|| (Year%4==0&& Year% -!=0)) {days[1] = in;//If it is a leap year, the2The value of the month is changed to inOr no action is done, the default -Days}//define an shaping variable to save the date first, and then pass forLoop consecutively plus the number of days of all full months before the current given month, and finally return the resultintDaysofyear = day; for(inti =0; I < month-1;    i++) {daysofyear + = Days[i]; }returnDaysofyear;}

C language----functions

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.