Click the blue words to follow us
This article introduces the definition of functions in C language and the use of sub-functions. It then explains variable scope, defines global variables, static variables, read-only variables, variable initialization, and so on.
1. Function Definition
// Define a function
int func(int a, int b) { }
<return type> <function name>(<parameter list>,....) { function body; }
<return type>: The return type can be any type supported by C language. Basic data types, pointers, structures, enumerations…
If the function does not need to return a value after execution, the return type can be declared as void.
Function Name: Cannot conflict with library function names, and the naming rules are the same as those for variable naming.
Parameter List: The parameters passed to the function during execution, defined in the same way as the return type.
If there are multiple parameters, they can be separated by commas.
Return Value: If the function needs to return data to the caller after execution, it can use the return statement, which can only return one value.
#include <stdio.h>
int func(int, int); // Declare func function
//int func(int a, int b); // Declare func function
int main(void) { int a; a = func(12.34, 56.78); // The parameter passed will be converted to integer
printf("a=%d\n", a); return 0;}
// Define a function
int func(int a, int b) { int c; c = a + b; return c; // Return result to the caller}
2. Function Exercises
(1) Write a function to determine whether a given year is a leap year. (Condition: divisible by 4 and not divisible by 100 or divisible by 400)
#include <stdio.h>
int func_year(int year); // Declare function
int main(void) { int year; // C89 standard int err = 0; printf("Enter a year:"); scanf("%d", &year); err = func_year(year); // Call function
if (err == 1) { printf("Leap Year!\n"); } else if (err == 0) { printf("Common Year!\n"); } else { printf("Invalid year input!\n"); } return 0;}
(2) This function is called by the main function to determine common and leap years.
/* Function functionality: Determine common and leap years
Return value: 0 indicates common year, 1 indicates leap year, negative number indicates error */
int func_year(int year) { if (year < 1900) return -1; // Add a restriction condition
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 1; }
return 0;}
(3) Input a Fahrenheit temperature and output the Celsius temperature. The calculation formula is (Fahrenheit – 32) × 5 ÷ 9, with the result rounded to two decimal places.
#include <stdio.h>
float func_temp(float temp); // Declare function
int main(void) { float temp; printf("Enter a temperature value:"); scanf("%f", &temp); printf("temp=%.2f\n", func_temp(temp)); return 0;}
/* Function functionality: Calculate temperature
Return value: Celsius */
float func_temp(float temp) { //(Fahrenheit - 32) × 5 ÷ 9 return (temp - 32) * 5 / 9.0;}
(4) Encapsulate a function to print the following pattern: Palindrome triangle, the parameter can determine the number of rows.
1
121
12321
1234321
(5) Calculate percentage and automatic data conversion
#include <stdio.h>
int main(void) { float data; data = (10 / 60.0) * 100; // During the operation, one of the numbers needs to be a floating-point number for floating-point storage
printf("data=%.0f%%\n", data); return 0;}
(6) Function return value example: Restricted range
#include <stdio.h>
int func(int a);
int main(void) { printf("%d\n", func(200)); return 0;}
int func(int a) { return (a == 100); // Restricted range value is 0 and 1}
3. Variable Scope
3.1 Global Variables and Local Variables
const int c; // Define read-only variable
static int b; // Define static variable
Note: The scope of variable definitions is divided into global variables and local variables.
1. Local variable and global variable names can be the same.
2. If the local variable name is the same as the global variable name, the local variable takes precedence.
#include <stdio.h>
void func(int);
int data = 123; // Global variable (shared variable)
int main(void) { int data = 456; // Local variable printf("data1=%d\n", data); func(666); return 0;}
void func(int data) { printf("data2=%d\n", data);}
3.2 Read-Only Variables
#include <stdio.h>
void func(int);
const int data = 888; // Read-only variable
int main(void) { //data = 666; // Error
printf("%d\n", data); return 0;}
void func(int data) { printf("data2=%d\n", data);}
3.3 Static Variables
// Static variable test
#include <stdio.h>
int func(void);
int main(void) { int i, data; for (i = 0; i < 5; i++) { data = func(); } printf("data=%d\n", data); return 0;}
int func(void) { //int data = 0; // Local variable, lifecycle ends with function call.
static int data = 0; // Static variable, lifecycle is the same as main function.
//static int data = 0 only takes effect the first time
data++; //data = data + 1 ,1 return data;}
3.4 Static Global Variables
#include <stdio.h>
//int data; Global variable, can be referenced in other .c files
static int data = 0; // Static global variable-----local variable
// Static global variable: Indicates that the data variable cannot be referenced by other files.
// Prevent global variable renaming.
int main(void) { return 0;}
3.5 Static Functions
#include <stdio.h>
static int func(void);
int main(void) { func(); return 0;}
// Define a static function, indicating that the function can only be used in this file.
static int func(void) { printf("123\n");}
3.6 Variable Initialization Values
#include <stdio.h>
static int data1; int data2;
int main(void) { int data3; // Local variable static int data4; printf("data1=%d\n", data1); //0 printf("data2=%d\n", data2); //0 printf("data3=%d\n", data3); //unknown value printf("data4=%d\n", data4); //0
int cnt; //cnt++; /* for(i=0;i < 5; i++) { if(xxxx) data3 |= 0x1; data3 <<= 1; }*/ return 0;}
*Disclaimer:This article is compiled from the internet, and the copyright belongs to the original author. If there is any error in the source information or infringement of rights, please contact us for deletion or authorization matters.

Click to read the original text to learn more