C language programming-function

Function definition A
function is named, an independent C code segment that performs a special task, and you can choose whether to return a value to the program that calls it.
1. The function is named
2. The function is independent
3. The function can be sent to the program that called it. returns a value
function of the classification
standard library function
ANS / ISO standard library functions defined:
when used, the header file must define the function include it in the beginning of the program
to third library functions
not within the standard, to the extension of the C language
from Function definition Function
definition of user-
defined
function function return value type function name (formal parameter)
{ function body local variable }


例子:
int GetMax(int x,  int y){
    
     //定义一个求最大值的函数
 int result;
 if (x>y)
 {
    
    
  result =x;
 }
 else{
    
    
  result =y;
 }
 return 0;   //函数的出口 只能返回一个值
}

The benefits of functional programming
1. Information hiding
2. For the user of the function, it is not allowed to know how the function works
3. It means to understand its interface with the outside world (Interface)
4. Hide the specific implementation details of the function from the outside world, As long as the externally provided interface remains unchanged, it will not affect the use of the function.
5. It is convenient to realize the reuse of
functions and the basic method of modular programming . The basic method of function call is that the
main function calls the called function by the function name and
does not return a value. Body sentence

void DisplayMeou(void)
{
    
    
 printf("1.input");
 printf("2.input");
 printf("0.input");
 return ;
 }
 //调用
int mian()
{
    
    
 DisplayMeou();
 return 0;
 }

The caller calls the function by the function name

int Average(int x,int y){
    
    
 int result;
 result =(x+y)/2;
 return result;
 //调用
int main(){
    
    
}
int a=12,b=24,ave;
ave =Average (a,b);
return 0;
}

1. The caller calls the function through the function name
2. When there is a return value, it can be placed in an assignment expression statement
3. It can also be placed in a function call statement as a parameter of another function

Formal parameters The actual parameters
of the parameters when the function is defined. The parameters when the function is called. Each time the function is called. 1. Protect the scene and allocate memory for the local variables (including formal parameters) in the function. 2. Assign a copy of the actual parameter value to the formal parameter. , One-way value transfer (actual parameter -> formal parameter) 3. The number, type and order of actual participating formal parameters should be the same when exiting from the function 1. The return address saved in the function call stack is returned to the place of this function call 2. Return the function value to the calling function, and at the same time return control to the caller 3. Recover the memory allocated to all variables (including formal parameters) in the function









例子://函数定义出现在函数调用之前
#include <stdio.h>
int Average(int x,int y){
    
    
 int result;
 result =(x+Y)/2;
 return result;
}
int int main(int argc, char const *argv[])
{
    
    
 int a, b, ave;
 scanf("%d,%d",&a,&b);
 ave =Average(a,b);
 printf("%d\n",ave );
 return 0;
}
例子://函数定义出现在函数调用之后
#include <stdio.h>
int Average(int x, int y); //函数原型声明
int int main(int argc, char const *argv[])
{
    
    
 int a, b, ave;
 scanf("%d,%d",&a,&b);
 ave =Average(a,b);
 printf("%d\n",ave );
 return 0;
}
int Average(int x,int y){
    
    
 int result;
 result =(x+Y)/2;
 return result;
}

The difference between function prototype and function definition
Function definition
1. Refers to the establishment of function function
2. Has function body
3. Is a complete and independent unit
4. Allocate memory, load function into memory
Function prototype
1. For function name, return value type, The formal parameter type is declared
2. Does not include the function body
3. It is a statement that ends with a semicolon and plays a declarative role
4. No memory is allocated, only a reference is kept

#include <stdio.h>
long Fact(int n);
int main(int argc, char const *argv[])
{
    
    
 int m;
 long result;
 printf("Input m:" );
 scanf("%d",&m);
 ret =Fact(m);
 if (ret == -1)
 {
    
    
  printf("your number is error\n");
 }
 else{
    
    
  printf("%d\n",m,ret );
 }
  return 0;
}
int Fact(int n){
    
    
 int i;
 long result =1;
 if (n<0)
 {
    
    
  result -1;
 }
 else{
    
    
  for (int i = 2; i < =n; i++)
  {
    
    
   result *=i;
  }
  return result;
 }
 }

Principles of function design
1. Function size should be small
2. Function function should be single
3. Function interface definition should be clear.
Assert
the correctness of the assumption in the test program.
If the assumption is violated, interrupt the program execution
. Conditions of use:
1. Check various assumptions program correctness
2. test to confirm or some situation does not happen will not happen
fundamental principle
1. use cases capture the assertion should not or can not happen
2. each test assert only one condition
only It is used to debug the program and cannot be used as a function of the program.
Use assertions to facilitate the discovery of errors in the program and will not affect the efficiency of program execution. It
cannot replace conditional filtering.

例子:计算某个数的阶乘
#include  <stdio.h>
#include <assert.h>  //定义断言的宏
unsigned long Fact (unsigned int n);
int main(int argc, char const *argv[])
{
    
    
 int m;
 do{
    
    
  printf("Input m(m>=0):" );
  scanf("%d",&m);
 }while (m<0);
 assert(m>=0);  //如果程序为真,则继续运行,如果为假,中断程序
 printf("%d!= % lu\n",m,Fact(m) );
 return 0;
 }
unsigned long Fact (unsigned int n)
{
    
    
 unsigned int i;
 unsigned long result  =1;
 for (i = 2; i < =n; i++)
 {
    
    
  result *=i;
 }
 return result;
}

Inline functions
are usually very short. The compiler will try to execute inline functions in the fastest way possible, and the code segment to be executed will be put into the main calling function

Structured programming
1. It is easier to write a structured program, and it is easier to break a complex problem into a number of simple small tasks, each task is completed by a function
2. It is easier to call a structured program
3. You can reuse code segments To save time

Guess you like

Origin blog.csdn.net/weixin_45743004/article/details/104223692