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

What are the custom parameters and calling methods of C language function classification?

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

Share

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

This article mainly explains "what are the custom parameters and calling methods of C language function classification". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C language function classification custom parameters and calling methods are what" it!

1. What is the function?

When it comes to functions, the first thing that comes to mind is the function in mathematics, so what is the function in C language? Let's take a look at it.

Definition of function in Wikipedia: subroutine

In computer science, a subroutine (English: Subroutine, procedure, function, routine, method, subprogram, callable unit) is a part of code in a large program that consists of one or more statement blocks. It is responsible for accomplishing a specific task and is relatively independent compared with other codes. There are generally input parameters and return values that provide encapsulation of the process and hiding of details. This code is usually integrated into a software library.

2. Classification of functions in C language 2.1 Library functions 2.1.1 Why should there be library functions

1. We know that when we learn C programming, we can't wait to know the result after a code has been written and want to print the result to our screen. At this time, we will frequently use a function: print the information to the screen according to a certain format (printf).

two。 In the process of programming, we will frequently do some string copy work (strcpy).

3. In programming, we also calculate, always calculate the k power of n such operations (pow).

Like the basic functions we described above, they are not business code. In the process of development, every programmer may use it. In order to support portability and improve the efficiency of the program, the basic library of C language provides a series of similar library functions to facilitate programmers to develop software.

2.1.2 what is a library function

So what are library functions? To take a simple example, library functions are functions that the C language itself has defined for us, and as programmers we can use them directly, like printf () and scanf ().

Note: the header file must be included when using the library function. For example, when we use printf () and scanf (), we have to refer to the stdioh header file, which is the # include we usually write.

2.1.3 can the main function only be main ()?

Many friends are confused, so what is the main function? Why is the main function called main function? Do we have to use the main () function? Next, I would like to relieve everyone of this doubt!

First of all, let me give you a conclusion. The C language defaults to main as the name of the main function, but the name of the main function is not necessarily main (). In fact, we can set the name of the main function ourselves. C language provides # pragma comment () to set the name of the main function. Interested partners can try it by themselves. In this place, like yo, ah, tell everyone. The name of the main function as the entry of the program does not have to be main (). I hope you can remember this! As for why the main function is called the main () function, this is the default of the C language. If you have to force a wave of interpretation, the English consciousness of main () is the main meaning.

2.1.4 Common library functions

IO function

String operation function

Character operation function

Memory operation function

Time / date function

Mathematical function

Other library functions

This place will not be expanded to talk about, but will be explained in detail when it is used later.

2.2 Custom function 2.2.1 what is a Custom function

Custom functions are functions defined by programmers for specific functions in the first place! For example, if we want to add two numbers, the add () function we define belongs to the custom function.

2.2.2 Why should there be a custom function

Custom functions, like library functions, have function names, return value types, and function parameters.

But the difference is that we design all these by ourselves. This gives programmers a lot of room to play.

2.2.3 composition of the function ret_type fun_name (para1, *) {statement;// statement item} ret_type return type fun_name function name para1 function parameter 2.2.4 example

(1) write a function to find the maximum of two integers.

The design of # include / / get_max function int get_max (int x, int y) {return (x > y)? (X): (y);} int main () {int num1 = 10; int num2 = 20; int max = get_max (num1, num2); printf ("max =% d\ n", max); return 0;}

This place explains the trinomial operator, that is, the above (x > y)? (X): (y). What this place means is that if x > y is true, the value of x is returned, and otherwise, the value of y is returned, thus achieving the goal of finding the maximum value!

(2) write a function to find the sum of two numbers.

Design of # include / / get_add function int get_add (int x, int y) {return (x + y);} int main () {int num1 = 10; int num2 = 20; int sum = get_add (num1, num2); printf ("sum =% d\ n", sum); return 0;} 3. Parameters of the function 3.1 actual parameters (arguments)

The parameters actually passed to the function are called arguments. Arguments can be: constants, variables, expressions, functions, etc.

Note: why can it be a function? Because some functions are returned by the value, they can naturally act as arguments.

No matter what type of quantity the arguments are, they must have certain values when making a function call in order to pass these values to the formal parameter.

3.2 formal parameters (formal parameters)

Formal parameters refer to the variables in parentheses after the function name, because formal parameters are instantiated only when the function is called (allocating memory units), so they are called formal parameters. Formal parameters are automatically destroyed when the function call is complete. Therefore, formal parameters are only valid in functions.

Why do you say that? Next, let me give you an example!

For example, we want to exchange the values of two variables:

# include void swap (int x, int y) {int temp = x; x = y; y = temp;} int main () {int num1 = 10; int num2 = 20; swap (num1,num2); printf ("num1 =% d\ nnum2 =% d", num1,num2); return 0;}

Here is the running result of the code:

Obviously, the purpose of the exchange has not been achieved, which proves that the formal parameters are automatically destroyed after the function call is completed, that is, after we pass num1 and num2 to x and y, although we exchange x and y in the function example, because x and y are destroyed after the swap function call is completed, that is, the exchange between num1 and num2 is not really realized.

So we can simply think that the instantiation of the formal parameter is actually equivalent to a temporary copy of the actual parameter.

4. Function call 4.1 pass value call

The formal parameters and arguments of the function occupy different memory blocks, and the modification of the parameters will not affect the parameters. Our above example of exchange is a value call, which does not really achieve the purpose of exchanging the values of two variables!

4.2 address invocation

An addressing call is a way to call a function by passing the memory address of a variable created outside the function to the function parameter.

This way of passing parameters allows the function to establish a real relationship with the variables outside the function, that is, the interior of the function can be directly manipulated as variables outside the function.

Similarly, we will use the previous example of exchanging the values of two variables to give you an example!

# include void swap (int * x, int * y) {int temp = * x; * x = * y; * y = temp;} int main () {int num1 = 10; int num2 = 20; swap (& num1, & num2); printf ("num1 =% d\ nnum2 =% d", num1,num2); return 0;}

Why did this happen in this place? We will explain it in detail when we learn the pointer section later!

4.3 exercise 4.3.1. Write a function to determine whether a year is a leap year. # includeint is_leap_year (int year) {if ((year% 4 = = 0 & & year% 100! = 0) | | year% 400 = = 0) {return 1;} else {return 0;}} int main () {int year = 0; int flag = 0; scanf ("% d", & year) Flag=is_leap_year (year); if (1 = = flag) {printf ("Leap year!") ;} else {printf ("not a leap year") ;} return 0;} 4.3.2. Write a function to determine whether a number is a prime or not. # include#includeint is_prime_num (int n) {int flag = 0; int I = 0; for (I = 2; I right) {return-1;}} int main () {int arr [] = {1, 2, 3, 5, 6, 7, 7, 9, 10}; int k = 7; int size = 0; size = sizeof (arr) / sizeof (arr [0]) Int ret = 0; ret = find_num (arr, size,k); if (- 1 = = ret) {printf ("not found!") ;} else {printf ("found! subscript% d", ret);} return 0;} so far, I believe you have a deeper understanding of "C language function classification custom parameters and calling methods". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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