# Chapter 7 C language functions

## 7.1 what is a function? concept

We encapsulate (package) the commonly used code into an independent module in a fixed format. As long as we know the name of this module, we can reuse it. This module is called Function.

Explain the encapsulation of the function and some precautions with the function of comparing the size of the string.

Library functions and custom functions

The functions of C language are called library functions. Library is a basic concept in programming. It can be simply regarded as a set of functions, often a folder on disk. The library of C language is called Standard Library, and the library developed by other companies or individuals is called third party library.

In addition to library functions, we can also write our own functions to expand the functions of the program. Self written functions are called user-defined functions. Custom functions and library functions are written and used in exactly the same way, but written by different institutions.

parameter

Parameters are the data to be processed by the function, and the data or variables contained in parentheses after the function name

For example:

The strlen parameter is used to calculate the length of strlen.

The function of formal parameter and argument is to pass data. When a function call occurs, the value of the argument will be passed to the formal parameter.

The formal parameter variable will allocate memory only when the function is called. After the call, the memory will be released immediately. Therefore, the formal parameter variable is only valid inside the function and cannot be used outside the function. (function external???)

The data transfer in the function call is one-way. You can only pass the value of the argument to the formal parameter, but not the value of the formal parameter to the actual parameter in the opposite direction; In other words, once the data transfer is completed, the argument and the formal parameter are no longer related. Therefore, the change of the value of the formal parameter during the function call will not affect the argument.

Although formal parameters and arguments can have the same name, they are independent of each other and do not affect each other, because the arguments are valid outside the function, while the formal parameters are valid inside the function.

#include <stdio.h> //Calculate the value from m to n int sum(int m, int n) { int i; for (i = m + 1; i <= n; ++i) { m += i; } return m; } int main() { int m, n, total; printf("Input two numbers: "); scanf("%d %d", &m, &n); total = sum(m, n); printf("m=%d, n=%d\n", m, n); printf("total=%d\n", total); return 0; }

Operation results:

Input two numbers: 1 100

m=1, n=100

total=5050

Return value

The return value is the execution result of the function

For example:

char str1[ ] = "C Language";

int len = strlen(str1);

The processing result of strlen() is the length of the string str1, which is an integer. We receive it through the len variable.

The return value of the function has fixed data types (int, char, float, etc.), and the variable types used to receive the return value should be consistent.

There can be multiple return statements, which can appear anywhere in the function body, but only one return statement can be executed each time the function is called, so there is only one return value

//Returns the larger of two integers int max(int a, int b){ if(a > b){ return a; }else{ return b; } }

Judge whether it is prime

#include <stdio.h> int prime(int n){ int is_prime = 1, i; //Once n is less than 0, it does not meet the conditions, and there is no need to execute the following code, so end the function in advance if(n < 0){ return -1; } for(i=2; i<n; i++) //Note the value range of i { if(n % i == 0) { is_prime = 0;//It is not prime numbers that satisfy the above conditions break; //Early termination of cycle } } return is_prime; } int main(){ int num, is_prime; scanf("%d", &num); is_prime = prime(num); if(is_prime < 0){ printf("%d is a illegal number.\n", num); }else if(is_prime > 0){ printf("%d is a prime number.\n", num); }else{ printf("%d is not a prime number.\n", num); } return 0; }

Once the function encounters the return statement, it will return immediately, and all subsequent statements will not be executed. From this point of view, the return statement also has the function of forcibly ending the execution of the function.

Change the above code so that return is not followed by any data:

#include <stdio.h> void prime(int n){ int is_prime = 1, i; if(n < 0){ printf("%d is a illegal number.\n", n); return; //return is not followed by any data } for(i=2; i<n; i++){ if(n % i == 0){ is_prime = 0; break; } } if(is_prime > 0){ printf("%d is a prime number.\n", n); }else{ printf("%d is not a prime number.\n", n); } } int main(){ int num; scanf("%d", &num); prime(num); return 0; }

# 7.2 function definition

The process of encapsulating code segments into functions is called function definition.

Definition of parameterless function in C language

If the function does not receive the data passed by the user, it can be defined without parameters. (that is, when there is no input)

For example, define a function to calculate the result from 1 to 100:

int sum(){

int i, sum=0;

for(i=1; i<=100; i++){

sum+=i;

}

return sum;

}

Return is a keyword in C language. It can only be used in functions to return processing results.

Complete the above code:

#include <stdio.h> int sum(){ int i, sum=0; for(i=1; i<=100; i++){ sum+=i; } return sum; } int main(){ int a = sum(); printf("The sum is %d\n", a); return 0; }

Functions cannot be nested. Main is also a function definition, so you should put sum outside main. Functions must be defined before use, so sum should be placed in front of main.

main is a function definition, not a function call.

Function with no return value

Some functions do not need a return value, or the return value type is uncertain (very rare), so it can be represented by void, for example:

void hello(){ printf ("Hello,world \n"); //No return statement is required if there is no return value }

void in C language means * * "empty type" or "no type" * *. In most cases, it means that there is no return statement.

Definition of parametric function in C language

The parameter is also a variable in nature, and the type and name should be specified when defining.

The data is passed to the inside of the function through parameters for processing. After processing, it is notified to the outside of the function through the return value.

The parameters given during function definition are called formal parameters, which are called formal parameters for short; The parameters given during function call (that is, the data passed) are called actual parameters, which are referred to as actual parameters for short.

When a function is called, the value of the argument is passed to the formal parameter, which is equivalent to an assignment operation.

In principle, the type and number of arguments should be consistent with the formal parameters.

#include <stdio.h> int sum(int m, int n){ int i, sum=0; for(i=m; i<=n; i++){ sum+=i; } return sum; } int main(){ int begin = 5, end = 86; int result = sum(begin, end); printf("The sum from %d to %d is %d\n", begin, end, result); return 0; }

Functions cannot be nested

You cannot define another function in one function

#include <stdio.h> void func2(){ printf("C Language little white monster"); } void func1(){ printf("http://c.biancheng.net"); func2(); } int main(){ func1(); return 0; }

Func1(), func2(), and main() are parallel and cannot be located inside anyone. To achieve the purpose of "calling func2() when calling func1()", you must define func2() outside func1() and call func2() inside func1().

Examples

Nested calls to functions

Title Description:

Using factorial function to calculate combination number

This problem requires the realization of two user-defined functions: the combination number function and the factorial function.

Function interface definition:

int comb(int m,int n);

int fac(int x);

Example of referee test procedure:

Sample input:

6 3

Sample output:

20

int fac(int x) { int i,s=1; for(i=0;i<x;i++) { s=s*i; } return s; } int comb(int m,int n) { int c; c=fac(m)/(fac(n)*fac(m-n)); return c; } #include<stdio.h> int fac(int x); int comb(int m,int n); int main(void) { int a,b; while(scanf("%d %d",&a,&b)!=EOF) printf("%d\n",comb(a,b)); }

The wrong code will be corrected tomorrow

[example] calculate sum = 1+ 2! + 3! + … + (n-1)! + n!

#include<stdio.h> int f(int n) { int i,m=1; for(i=1;i<=n;i++) { m=m*i; } return m; } int S(int n) { int i,s=0; for(i=1;i<=n;i++) { s=s+f(i); } return s; } int main() { int n; scanf("%d",&n); printf("%d",S(n)); }