在 C 语言中,我们可以将一个大程序分解成基本的构建块,称为函数。函数包含由 {} 括起来的一组编程语句。可以多次调用函数以提供 C 程序的重用性和模块化性。换句话说,我们可以说函数的集合创建了一个程序。函数在其他编程语言中也被称为过程子例程

C 函数的优点

C 函数具有以下优点:

  • 通过使用函数,我们可以避免在程序中重复编写相同的逻辑/代码。
  • 我们可以在程序中的任何地方多次调用 C 函数。
  • 当将大型 C 程序分解为多个函数时,可以更轻松地跟踪程序。
  • 重用性是 C 函数的主要优点。
  • 然而,函数调用在 C 程序中总是有一定的开销。

函数的三个方面

C 函数有三个方面。

  • 函数声明:函数必须在 C 程序中全局声明,以告诉编译器函数名称、函数参数和返回类型。
  • 函数调用:函数可以从程序中的任何地方调用。函数调用和函数声明的参数列表不能不同。在函数调用中,我们必须传递与函数声明中声明的参数数量相同的参数。
  • 函数定义:它包含要执行的实际语句。当调用函数时,控制流会进入这个最重要的方面。在这里,我们必须注意只能从函数中返回一个值。
序号C 函数方面语法
1函数声明返回类型 函数名(参数列表);
2函数调用函数名(参数列表)
3函数定义返回类型 函数名(参数列表) {函数体;}

在 C 语言中创建函数的语法如下:

  1. 返回类型 函数名(数据类型 参数...) {
  2. // 要执行的代码
  3. }

函数的类型

C 程序中有两种类型的函数:

  1. 库函数:这些函数在 C 头文件中声明,如 scanf()、printf()、gets()、puts()、ceil()、floor() 等。
  2. 用户自定义函数:这些函数由 C 程序员创建,以便可以多次使用。它减少了大型程序的复杂性并优化了代码。

c-function.jpg

返回值

C 函数可以有返回值,也可以没有返回值。如果函数不需要返回任何值,请使用 void 作为返回类型。

让我们看一个简单的没有返回值的 C 函数的示例。

没有返回值的函数示例:

#include <stdio.h>

void hello() {
    printf("hello c");
}

int main() {
    hello();
    return 0;
}

// 输出结果:
// hello c

如果你想从函数中返回任何值,你需要使用诸如int、long、char等数据类型。返回类型取决于函数要返回的值。

让我们看一个简单的C函数示例,它从函数中返回int值。

带有返回值的函数示例:

#include <stdio.h>

int get() {
    return 10;
}

int main() {
    int result = get();
    printf("%d", result);
    return 0;
}

// 输出结果:
// 10

在上面的示例中,我们要返回值10,因此返回类型是int。如果你想返回浮点数值(例如10.2、3.1、54.5等),则需要使用float作为方法的返回类型。

#include <stdio.h>

float get() {
    return 10.2;
}

int main() {
    float result = get();
    printf("%f", result);
    return 0;
}

// 输出结果:
// 10.200000

现在,你需要调用函数来获取函数的值。

函数调用的不同方面

一个函数可能接受任意数量的参数,也可能不接受任何参数。它可能返回任意值,也可能不返回任何值。基于这些事实,函数调用有四个不同的方面。

  • 无参数且无返回值的函数
  • 无参数且有返回值的函数
  • 有参数且无返回值的函数
  • 有参数且有返回值的函数

无参数且无返回值的函数示例

示例1:

#include <stdio.h>

void printName();

void main() {
    printf("Hello ");
    printName();
}

void printName() {
    printf("Javatpoint");
}

// 输出结果:
// Hello Javatpoint

示例2:

#include <stdio.h>

void sum();

void main() {
    printf("\nGoing to calculate the sum of two numbers:");
    sum();
}

void sum() {
    int a, b;
    printf("\nEnter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("The sum is %d", a + b);
}

// 输出结果:
// Going to calculate the sum of two numbers:
//
// Enter two numbers: 10
// 24
//
// The sum is 34

示例1

#include<stdio.h> 
void printName(); 
void main () 
{ 
  printf("Hello "); 
  printName(); 
} 
void printName() 
{ 
  printf("Javatpoint"); 
}

输出

Hello Javatpoint

示例2

#include<stdio.h> 
void sum(); 
void main() 
{ 
  printf("\nGoing to calculate the sum of two numbers:"); 
  sum(); 
} 
void sum() 
{ 
  int a,b;  
  printf("\nEnter two numbers"); 
  scanf("%d %d",&a,&b);  
  printf("The sum is %d",a+b); 
}

输出

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

无参数有返回值函数示例

示例1

#include<stdio.h> 
int sum(); 
void main() 
{ 
  int result;  
  printf("\nGoing to calculate the sum of two numbers:"); 
  result = sum(); 
  printf("%d",result); 
} 
int sum() 
{ 
  int a,b;  
  printf("\nEnter two numbers"); 
  scanf("%d %d",&a,&b); 
  return a+b;  
}

输出

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

示例2:计算正方形的面积

#include<stdio.h> 
int square(); 
void main() 
{ 
  printf("Going to calculate the area of the square\n"); 
  float area = square(); 
  printf("The area of the square: %f\n",area); 
} 
int square() 
{ 
  float side; 
  printf("Enter the length of the side in meters: "); 
  scanf("%f",&side); 
  return side * side; 
}

输出

Going to calculate the area of the square 
Enter the length of the side in meters: 10 
The area of the square: 100.000000

带参数无返回值函数示例

示例1

#include<stdio.h> 
void sum(int, int); 
void main() 
{ 
  int a,b,result;  
  printf("\nGoing to calculate the sum of two numbers:"); 
  printf("\nEnter two numbers:"); 
  scanf("%d %d",&a,&b); 
  sum(a,b); 
} 
void sum(int a, int b) 
{ 
  printf("\nThe sum is %d",a+b);   
}

输出

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

示例2:计算五个数的平均值

#include<stdio.h> 
void average(int, int, int, int, int); 
void main() 
{ 
  int a,b,c,d,e;  
  printf("\nGoing to calculate the average of five numbers:"); 
  printf("\nEnter five numbers:"); 
  scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); 
  average(a,b,c,d,e); 
} 
void average(int a, int b, int c, int d, int e) 
{ 
  float avg;  
  avg = (a+b+c+d+e)/5;  
  printf("The average of given five numbers : %f",avg); 
}

输出

Going to calculate the average of five numbers:
Enter five numbers:10 
20
30
40
50
The average of given five numbers : 30.000000

带参数有返回值函数示例

示例1

#include<stdio.h> 
int sum(int, int); 
void main() 
{ 
  int a,b,result;  
  printf("\nGoing to calculate the sum of two numbers:"); 
  printf("\nEnter two numbers:"); 
  scanf("%d %d",&a,&b); 
  result = sum(a,b); 
  printf("\nThe sum is : %d",result); 
} 
int sum(int a, int b) 
{ 
  return a+b; 
}

输出

Going to calculate the sum of two numbers:
Enter two numbers:10
20 
The sum is : 30   

示例2:检查一个数是奇数还是偶数

#include<stdio.h> 
int even_odd(int); 
void main() 
{ 
  int n,flag=0; 
  printf("\nGoing to check whether a number is even or odd"); 
  printf("\nEnter the number: "); 
  scanf("%d",&n); 
  flag = even_odd(n); 
  if(flag == 0) 
  { 
    printf("\nThe number is odd"); 
  } 
  else  
  { 
    printf("\nThe number is even"); 
  } 
} 
int even_odd(int n) 
{ 
  if(n%2 == 0) 
  { 
    return 1; 
  } 
  else  
  { 
    return 0; 
  } 
}

输出

Going to check whether a number is even or odd
Enter the number: 100
The number is even

C 库函数

C 库函数是 C 语言中的内置函数,它们被分组并放置在一个称为库的公共位置。这些函数用于执行特定的操作。例如,printf 是一个库函数,用于在控制台上打印输出。C 库函数是由编译器的设计者创建的。所有的 C 标准库函数都定义在不同的带有扩展名 .h 的头文件中。我们需要在程序中包含这些头文件,以使用这些头文件中定义的库函数。例如,要使用诸如 printf/scanf 等库函数,我们需要在程序中包含 stdio.h,它是一个包含所有与标准输入/输出有关的库函数的头文件。

下表列出了常用的头文件列表:

序号头文件描述
1stdio.h这是一个标准输入/输出头文件。它包含有关标准输入/输出的所有库函数。
2conio.h这是一个控制台输入/输出头文件。
3string.h它包含所有与字符串相关的库函数,如 gets()、puts() 等。
4stdlib.h这个头文件包含了所有通用的库函数,如 malloc()、calloc()、exit() 等。
5math.h这个头文件包含了所有与数学运算相关的函数,如 sqrt()、pow() 等。
6time.h这个头文件包含了所有与时间相关的函数。
7ctype.h这个头文件包含了所有字符处理函数。
8stdarg.h可变参数函数在这个头文件中定义。
9signal.h所有信号处理函数在这个头文件中定义。
10setjmp.h这个文件包含了所有跳转函数。
11locale.h这个文件包含了区域设置函数。
12errno.h这个文件包含了错误处理函数。
13assert.h这个文件包含了诊断函数。

标签: c语言, c语言教程, c语言技术, c语言学习, c语言学习教程, c语言下载, c语言开发, c语言入门教程, c语言进阶教程, c语言高级教程, c语言面试题, c语言笔试题, c语言编程思想