← Back to Index
Research & Engineering Archive

LPC 5. Functions

By Jingnan Huang · January 28, 2025 · 1744 Words

Last Edit: 1/28/25

5.1 Functions
#

Function是用来执行特定任务的可重复调用恶的代码块,通过Modular Programming将复杂问题分解

void printStars(int numOfStars) {
    for (int star = 1; star <= numOfStars; star++) {
        printf("%c", '*');
    }
    printf("\n"); // to start a new line
}

5.1.1 Void
#

5.1.2 Function Prototype
#

void printPattern(int numOfRows);
void printStars(int numOfStars);

5.1.3 Order of execution
#

5.1.4 Another way to write functions
#

# include <stdio.h>

void printStars(int numOfStars) {
  for (int star = 1; star <= numOfStars; star++) {
    printf("%c", '*');
  }
  printf("\n");  // to start a newline 
}

void printPattern(int numOfRows) {
  for (int row = 1; row <= numOfRows; row++) {
    printStars(row);
  }
}

int main(void) {
  int lines;
  printf("Enter the number of lines in the pattern:");
  scanf("%d", &lines);
  prinntPattern(lies);
  return 0;
}

5.1.4.1 Error Case
#

#include <stdio.h>

void printPattern(int numOfRows) {
    for (int row = 1; row <= numOfRows; row++) {
        printStars(row);
    }
}

void printStars(int numOfStars) {
    for (int star = 1; star <= numOfStars; star++) {
        printf("%c", '*');
    }
    printf("\n"); // to start a new line
}

int main(void) {
    int lines;
    printf("Enter the number of lines in the pattern: ");
    scanf("%d", &lines);
    printPattern(lines);
    return 0;
}

5.2. Communicate from a function
#

5.2.1 Return a non-void variable type
#

#include <stdio.h>
int factorial(int n);

int main(void) {
    int number = 4;
    int result = factorial(number);
    printf("Factorial of %d: %d.\n", number, result);
    return 0;
}

int factorial(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
        fact = fact * i;
    }
    return fact;
}

5.2.2. Summary of syntax
#

#include <stdio.h>
// 函数原型(声明)
<return type> functionName(<type>);
int main(void) {
    // 调用有返回值的函数
    <type> variableName = functionName(<variable to pass>);
    // 调用无返回值的函数
    functionName(<variable to pass>);
    return 0;
}
// 函数实现(定义)
<return type> functionName(<type> <input parameter name>) {
    return <变量,类型与 <return type> 相同>;
}

5.3 Variable Scope
#

在前面的Loop单元中提到过 Variable Scope 的概念,即一个变量的定义范围,在前面提到的是loop中的variable只能作用在其循环当中,想要让其作用于循环外则需要在外部声明变量

int count;
for (count = 1; count <= n; count++) {
    printf("*");
}
count = 10;  // ✅ 正确,count 在整个函数内都可用
#include <stdio.h>
// 函数声明
double divideByTwo(double);
int main(void) {
    double n = 4.2, result;
    result = divideByTwo(n);
    printf("%lf,%lf", n, result)
    return 0;
}
// 函数定义
double divideByTwo(double n) {
    n = n / 2;
    return n;
}

5.4. Pass more values to a function
#

#include <stdio.h>
int median(int, int, int); // Prototype
int main(void) { // Main Function
  printf("The median of (%d, %d, %d) is %d\n", -105, -28, -73, median(-105, -28, -73));
  printf("The median of (%d, %d, %d) is %d\n", 0, -101, 98, median(0, -101, 98));
  printf("The median of (%d, %d, %d) is %d\n", -101, -67, 0, median(-101, -67, 0));
  return 0;
}
int median(int x, int y, int z) { // Function Body
  int result = 0;
  if ((x >= z && x <= y) || (x >= y && x <= z))
    result = x;
  else if ((y >= x && y <= z) || (y >= z && y <= x))
    result = y;
  else
    result = z;
  return result;
}