编程c语言中,向上取整函数

什么是功能? (What is a Function?)

A Function is a block of statements that performs a specific task of some kind. Every C program can be thought of as the Collection of these Functions. The below Example shows how we can write a simple function.

函数是执行某种特定任务的语句块。 每个C程序都可以视为这些函数的集合。 下面的示例显示了我们如何编写一个简单的函数。

Function Example

功能实例

In the above Example, we have made a function. After creating the Function we can call it from main(). Functions can also call each other. A function can even call itself. The Function which calls itself is known as Recursion.

在上面的示例中,我们做了一个函数。 创建函数后,我们可以从main()中调用它。 函数也可以互相调用。 函数甚至可以调用自身。 调用自身的函数称为递归。

可以从上述计划中得出的结论 (Conclusions that can be drawn from above Program)

  1. A C program is a Collection of one or more Functions.AC程序是一个或多个功能的集合。
  2. If a C program contains only one function, it must be main().如果C程序仅包含一个函数,则它必须是main()。
  3. If a C program contains more than one function, then one of them is main() because the program execution begins with main().如果C程序包含多个函数,则其中一个函数是main(),因为该程序的执行从main()开始。
  4. There can be as many functions in a program as possible.程序中可以有尽可能多的功能。

为什么需要功能? (Why do we need Functions?)

  • It divides the program into separate well-defined functions so that each function can be written and tested separately.它将程序划分为单独的定义明确的函数,以便可以分别编写和测试每个函数。
  • Understanding, coding and testing becomes very easy as they are separated.由于将它们分开,所以理解,编码和测试变得非常容易。
  • Writing functions avoids rewriting the same code over and over.编写函数可以避免一遍又一遍地重写相同的代码。
  • As the functions are divided the workload can also be divided among the programmers.由于功能被划分,工作量也可以在程序员之间分配。

C中的函数声明 (Function Declaration in C)

Function Declaration

功能声明

Basic Syntax:

基本语法:

return_data_type function_name (data_type var1, data_type var2, …..);

Where,

哪里,

function_name: the name for the function should be valid. It should be a meaningful name that should clarify what all tasks it will perform.

function_name函数的名称应有效。 它应该是一个有意义的名称,它应该阐明它将执行的所有任务。

return_data_type: it is used for specifying the data type of the value that is returned to the calling function after the processing.

return_data_type :用于指定处理后返回到调用函数的值的数据类型。

data_type var1, data_type var2: function arguments and their data types.

data_type var1,data_type var2 :函数参数及其数据类型。

返回数据类型 (Return Data Types)

The return value has a type as other values in C. It can be int, float, char or anything else. The type of return value determines the type of your function.

返回值的类型与C中的其他值相同。它可以是int,float,char或其他任何类型。 返回值的类型决定了函数的类型。

The default return type of function is int or integer.

函数的默认返回类型为int或integer。

Return Data Types

返回数据类型

In the above Program, we have 3 Functions:

在以上程序中,我们具有3个功能:

  1. multiply(): Its return type is int, therefore it will return an integer type value.multiple():其返回类型为int,因此它将返回整数类型值。
  2. print(): Its return type is void, therefore it will not return anything, it will simply execute the code within.print():其返回类型为void,因此它将不返回任何内容,仅执行其中的代码。
  3. divide(): Its return type is a float, therefore it will return a decimal type value.split():其返回类型为浮点型,因此它将返回一个十进制类型的值。

C中的函数定义 (Function Definition in C)

Whenever a function is defined, its space is allocated in the memory.

每当定义函数时,其空间就会分配到内存中。

Syntax:

句法:

return_data_type function_name (data_type var1, data_type var2, …..);
{…...................statements;…...................return (variable);
}

The statements written within the curly braces ({}) are the body of the function which contains the code to be performed.

花括号({})中编写的语句是包含要执行的代码的函数的主体。

在C中调用函数 (Calling a Function in C)

Function Working

功能运作

Whenever the function is invoked the compiler skips on to the called function for executing its statements. We mean that the control passes to the function The activity of main() is temporarily suspended.

无论何时调用该函数,编译器都会跳至被调用函数以执行其语句。 我们的意思是该控件传递给了函数main()的活动被暂时挂起。

Once the called function is executed the control of the program is passed back to the calling function.

一旦执行了被调用的函数,程序的控制权就会传回到调用函数。

在函数之间传递值 (Passing values between functions)

Passing Values In C

在C中传递值

In the above Program, the variable a, b, c are called actual arguments. The variables x, y, z are called as formal arguments.

在上面的程序中,变量a,b,c被称为实际参数。 变量x,y,z被称为形式参数。

在C中按值调用 (Call by Value in C)

In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.

在此参数传递方法中,将实际参数的值复制到函数的形式参数中,并将两种类型的参数存储在不同的存储位置中。 因此,在函数内部进行的任何更改都不会反映在调用者的实际参数中。

While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”.

在调用函数时,我们将变量的值传递给它。 这些功能称为“按值调用”。

C program to illustrate call by value

C程序说明按值调用

#include<stdio.h>void swapx(int x, int y); int main()
{ int a = 100, b = 200; // Pass by Values swapx(a, b); printf("a=%d b=%d\n", a, b); return 0;
}
void swapx(int x, int y)
{ int t; t = x; x = y; y = t; printf("x=%d y=%d\n", x, y);
}

Output:

输出

The actual values of a and b remain unchanged even after exchanging the values of x and y.

即使在交换x和y的值之后,a和b的实际值仍保持不变。

在C中通过引用调用 (Call by Reference in C)

While calling a function, instead of passing the values of variables, we pass the address of variables (pointers) to the function known as “Call By References.

在调用函数时,我们不传递变量的值,而是将变量(指针)的地址传递给称为“按引用调用”的函数。

Both the actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in the actual parameters of the caller.

实际参数和形式参数都指向相同的位置,因此在函数内部所做的任何更改实际上都会反映在调用者的实际参数中。

C program to illustrate Call by Reference

C程序说明按引用调用

#include<stdio.h>void swapx(int*, int*); int main()
{ int a = 100, b = 200; // Pass reference swapx(&a, &b); printf("a=%d b=%d\n", a, b); return 0;
} void swapx(int* x, int* y)
{ int t; t = *x; *x = *y; *y = t; printf("x=%d y=%d\n", *x, *y);
}

Output:

输出

Call By Refrence

Refreence致电

The actual values of a and b get changed after exchanging values of x and y.

在交换x和y的值之后,a和b的实际值将更改。

什么是递归函数? (What is a Recursive Function?)

A function which calls itself is called a Recursive function.

调用自身的函数称为递归函数

While using the recursive functions, it is important to be careful to define the exit condition from the function or then it may result in an infinite loop.

使用递归函数时,一定要小心,以定义函数的退出条件,否则可能导致无限循环。

They are used for calculating factorial of a number, Fibonacci series, etc.

它们用于计算数字,斐波那契数列等的阶乘。

#include<stdio.h>
int fib(int n)
{
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
} int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}

翻译自: https://www.journaldev.com/30509/functions-in-c-programming

编程c语言中,向上取整函数

编程c语言中,向上取整函数_C编程中的函数相关推荐

  1. c语言中值程序,编程c语言中,向上取整函数_C编程中的函数

    编程c语言中,向上取整函数 什么是功能? (What is a Function?) A Function is a block of statements that performs a speci ...

  2. 编程c语言中,向上取整函数_C编程中的函数–第3部分

    编程c语言中,向上取整函数 Read: Functions in C Programming – Part 2 So far we have learnt about the simplest use ...

  3. c语言中point的用法_C/C++中 *和amp;的爱恨情仇

    C++中&和*的用法一直是非常让人头疼的难点,课本博客上讲这些的知识点一般都是分开讲其用法的,没有详细的总结,导致我在这方面的知识结构格外混乱,在网上找到了一篇英文文章简单总结了这两个符号的一 ...

  4. c语言中sign的用法,Excel教程中sign函数用法和实例详解

    第一,sign函数用法说明 excel教程中sign函数用于返回数字的符号.正数为1,零为0,负数为-1. sign函数语法:SIGN(number) SIGN符号函数(一般用sign(x)表示)是很 ...

  5. python语言中的type是什么,python中type函数怎么用

    python中type函数怎么用 发布时间:2020-12-10 09:22:51 来源:亿速云 阅读:80 作者:小新 小编给大家分享一下python中type函数怎么用,希望大家阅读完这篇文章后大 ...

  6. java向上取整函数_java取整函数,向上取整函数Math.ceil()

    你知道java取整函数要怎样实现吗?下面要给大家分享的是java向上取整函数的相关内容,一起来了解一下具体的方法吧! java向上取整函数Math.ceil():double dividend = 7 ...

  7. math.ceil java_java向上取整函数Math.ceil()

    java向上取整函数Math.ceil() 百度Math.ceil()函数:"Math.ceil()是常见编程语言中的常用代码,ceil() 方法执行的是向上取整计算,它返回的是大于或等于函 ...

  8. c语言int16位数据范围,c语言中int取值范围为16位指的是什么16位

    1.c语言中int取值范围为16位指的是什么16位 计算机用二进制表示数值,最小单位就是位(bit),可以储存0或1,16位就是有16个储存0或1的位,其中左边第一位是符号位,0代表+ .1代表-. ...

  9. c语言 ——int16,c语言中int取值范围为16位指的是什么16位

    匿名用户 1级 2013-10-20 回答 1.c语言中int取值范围为16位指的是什么16位 计算机用二进制表示数值,最小单位就是位(bit),可以储存0或1,16位就是有16个储存0或1的位,其中 ...

最新文章

  1. 基于 Nginx+lua+Memcache 实现灰度发布
  2. linux删除过期文件
  3. 找单独出现的bit 数组
  4. IIS 7.0 安装SSL证书过程
  5. 使用freetype来显示中文汉字和英文字符
  6. 用php实现遍历目录
  7. 常用adb 命令整理
  8. python采集数据搭建小说网站_Python制作爬虫采集小说
  9. 分享一篇关于代理IP对于python爬虫有多重要
  10. 简历里计算机能力,简历上计算机能力怎么写
  11. 用Dim搭建轻量级媒体服务器
  12. python蒙特卡洛_Python:从零开始的汉密尔顿蒙特卡洛
  13. AsyncHttpClient 请求
  14. 树莓派学习【不定时更新】
  15. 走进小程序【八】微信小程序中使用【Vant组件库】
  16. C#常用算法实例(二)
  17. 听说Python成为世界性语言了? Python是怎么构建世界?字符串在哪里?(三)
  18. 智慧校园-实验室预约介绍
  19. Oracle查询用户权限
  20. nch photopad mac支持哪些文件格式?

热门文章

  1. 2016届毕业生-毕业设计的相关事项
  2. [WP8.1UI控件编程]SemanticZoom控件实现分组列表
  3. ASP.NET配置FCKeditor文本编辑器
  4. [转载] python中list与string的转换
  5. [转载] python更新numpy_Python numpy从1.6更新到1.8
  6. GPS NMEA0183协议解析(转载)
  7. java基本语法(运算符)
  8. leetcode - 4. Median of Two Sorted Arrays
  9. 数据库查询·聚合分支格式化日期·思维导图要点误点(含示例)
  10. android开发学习笔记系列(6)--代码规范