Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The definition of default parameters of C language function

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the definition of default parameters of C language function". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn the definition of default parameters of C language function.

Catalogue

one。 Brief introduction of function

1. Function declaration

two。 Function definition

3. Function call

4. Functional parameters and actual parameters

two。 Function default parameter

1. All default parameters of the function

two。 Function semi-default parameter

three。 Matters needing attention

one。 Brief introduction of function 1. Function declaration

The function declaration is just an empty shell, and there will be no specific function implementation, but define the implementation of the function to be implemented, such as:

Int sub (int x drawing int y); / / you only need to declare it, and you don't need to implement the function 2. Function definition

The definition of a function needs to implement the functions of this function, such as:

Int sub (int xmenint y) / needs to implement the function of this function {return (xmery);} 3. Function call

The function must be declared or defined before it is called, or an error will be reported: the identifier cannot be found, which is why calling the printf function in hello world must include the stdio.h header file.

4. Functional parameters and actual parameters

Formal parameters: when a function is defined or declared, the parameter list is a formal parameter

/ / when the function is declared, the XMagi y in the parameter list is all the formal parameter int sub (int x drawing int y); / / when the function is defined, the x Magi y in the parameter list is all the formal parameter int sub (int x direction int y) {return (XMuy);}

Arguments: when a function is called, the passed parameter list is all arguments

The int main (void) {/ / sub function is called, and the parameters passed are all arguments sub (1Magne2); return 0;} 2. Function default parameter

Default parameters: specify a default value for the function's parameters when declaring or defining the function. When calling a function, the default value is used if no argument is specified, otherwise the specified argument is used.

The default parameters are divided into two categories: full default parameters and semi-default parameters.

1. All default parameters of the function

All default parameters of a function: when a function is defined or declared, all formal parameters are defaulted, for example:

/ * / / @ Author: ape programming / / @ Blog (personal blog address): www.codersrc.com//@File:C language function default parameter / / @ Time 2021-06-23 08:00//@Motto: you can't reach thousands of miles without a long walk. Do not accumulate a small stream can not become a river and sea, the program of wonderful life needs to unremittingly accumulate! / * / # include / / XMagol y is the formal parameter of the function. If the function is called without setting x and y values, the x value defaults to 100. The y value defaults to 5int sub (int Xerox 100instruction int yellow5) {return (XMY) } int main (void) {int aq20; int bounded 10; / / variables an and b are the arguments printf of the sub function ("sub function calculation result =% d\ n", sub (a, b)); / / Note: we have not set the argument for the sub function, so the default is 100 yearly 5 printf ("sub function calculation result =% d\ n", sub ()); return 0 } / * output: sub function calculation result = 10sub function calculation result = 95 prime 2. Function semi-default parameter

Function semi-default parameters: when a function is defined or declared, only some of the formal parameters have default values, for example:

/ * / / @ Author: ape programming / / @ Blog (personal blog address): www.codersrc.com//@File:C language function default parameter / / @ Time 2021-06-23 08:00//@Motto: you can't reach thousands of miles without a long walk. Do not accumulate a small stream can not become a river and sea, the program of wonderful life needs to unremittingly accumulate! / * / # include / / XMagi y is the formal parameter of the function. If the function is called without setting the y value, the y value defaults to 5int sub (int x drawing int yellow5) {return (XMY). } int main (void) {int aq20; int bounded 10; / / variables an and b are the arguments to the sub function ("sub function calculation result =% d\ n", sub (a, b)); / / Note: we did not set the argument for the second parameter of the sub function, so the default is yard5 printf ("sub function calculation result =% d\ n", sub (a)); return 0 } / * output: sub function calculation result = 10sub function calculation result = 15 minutes / 3. Matters needing attention

1. Parameter to the default value can only be given from right to left

(that is, formal parameters with default values must be placed at the end), for example:

Void Funtest (int a, int bten int censor 30) * can void Funtest (int a = 10 minint int int c) * No void Funtest (int a = 10 minint int int censor 30) * No

two。 The default value must be constant or global variable

Thank you for your reading. the above is the content of "the definition of default parameters of C language function". After the study of this article, I believe you have a deeper understanding of the definition of default parameters of C language function. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report