c语言中值程序,编程c语言中,向上取整函数_C编程中的函数
編程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)
A C program is a Collection of one or more Functions.
AC程序是一個或多個功能的集合。
If a C program contains only one function, it must be main().
如果C程序僅包含一個函數,則它必須是main()。
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()開始。
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個功能:
multiply(): Its return type is int, therefore it will return an integer type value.
multiple():其返回類型為int,因此它將返回整數類型值。
print(): Its return type is void, therefore it will not return anything, it will simply execute the code within.
print():其返回類型為void,因此它將不返回任何內容,僅執行其中的代碼。
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程序說明按值調用
#includevoid 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程序說明按引用調用
#includevoid 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.
它們用于計算數字,斐波那契數列等的階乘。
#includeint 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语言中,向上取整函数_C编程中的函数的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: mac mamp修改php.ini,ma
 - 下一篇: 谷歌将关闭物联网开发平台 Android