Getting to know C language for the first time ——— 3

In this article, the blogger briefly talks about his understanding of functions and arrays. Details will be given later.

Article directory

1. Function

Two, the array

1. Array definition

2. The subscript of the array

3. The use of arrays

1. Function

A function can be understood as the code of a module to complete an independent function.

#include <stdio.h>
int main()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;
    printf("输入两个操作数:>");
    scanf("%d %d", &num1, &num2);
    sum = num1 + num2;
    printf("sum = %d\n", sum);
    return 0;
}

This code is written as a function, as follows:

#include <stdio.h>
int Add(int x, int y)//形式参数
{
    int z = x + y;
    return z;
}
int main()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;
    printf("输入两个操作数:>");
    scanf("%d %d", &num1, &num2);
    sum = Add(num1, num2);//实际参数
    printf("sum = %d\n", sum);
    return 0;
}

1. Definition
In Wikipedia, the definition of a function is called a subroutine.

(1) A certain part of code in a large program consists of one or more statement blocks. It is responsible for completing a specific task and is relatively independent from other codes.

(2) Generally, there are input parameters and return values, which provide the encapsulation of the process and the hiding of details.

2. Classification
(1) Library functions: Functions provided within the C language.

(2) User-defined functions: Functions written by oneself.

The actual parameters (actual parameters)
are the parameters that are actually passed to the function, called actual parameters. Actual parameters can be: constants, variables, expressions, functions, etc. When the function is called, they must all have definite values ​​in order to pass these values ​​to the formal parameters.

Formal parameters (formal parameters)
Formal parameters are variables in parentheses after the function name. Formal parameters are only instantiated (allocated memory units) when the function is called, so they are called formal parameters. Formal parameters are therefore only valid within functions.
 

Two, the array

1. Array definition

Array Definition: A collection of elements of the same type

int arr[10] = {1,2,3,4,5,6,7,8,9,10};//Define an integer array with up to 10 elements

int: the type of the array. arr: array name. [10]: There are several elements. ={}: Assign an initial value to the array.

2. The subscript of the array

The C language stipulates that each element of the array has a subscript, and the subscript starts from 0. Arrays can be accessed by subscripting.

like:

int arr[10] = {0}; //If the array has 10 elements, the subscript range is 0-9
int arr[10] 0 0 0 0 0 0 0 0 0 0
subscript 0 1 2 3 4 5 6 7 8 9

3. The use of arrays

 Let's print the arr array.

That's it for this time, more content bloggers will continue to update in the future!

I hope you will support me a lot!

If the blogger's article is helpful to you, please pay attention to it, like it, and support the blogger. Thank you for your attention and likes.

Guess you like

Origin blog.csdn.net/LXW0403/article/details/130057681