Home  >  Article  >  Backend Development  >  Detailed explanation of fun usage in C language

Detailed explanation of fun usage in C language

藏色散人
藏色散人Original
2020-05-30 11:17:0625477browse

Detailed explanation of fun usage in C language

Detailed explanation of the usage of fun in C language

The fun function is a custom C/C language function, function function Diverse. The function is called "function", which is the abbreviation of English function. It is generally used in examples and test questions, and is usually called in the main function.

In C/C language, the fun function is usually called by the main function. It refers to using fun to define a function (or method), so that it can be expressed as fun when referencing. For example, int fun(int x,int y), void fun(char* a, char* b) and so on. With the previous definition, you can call it in the main function, such as ans=fun(3,7); or fun(p1,p2);.

The fun function is a custom function. The word fun has no other meaning except that it is used to represent the function when it is called.

Example

C Example 1

#include<iostream>
using namespace std;
int fun(int x,int y){//这里的fun函数表示计算x的y次幂
    int result=1;
    for(int i=1;i<=y;i++){
        result*=x;
    }
    return result;
}
int main(){
    int ans;
    ans=fun(3,7);//调用fun函数计算3的7次幂
    cout<<ans<<endl;//输出2187
    return 0;
}

C Example 2

#include<iostream>
using namespace std;
//这里的fun函数交换两个指针;其中char*&中*表示指针,&表示引用参数传递
void fun(char*& a,char*& b){
    char* c=a;
    a=b;
    b=c;
}
int main(){
    char* p1;
    char* p2;
    p1="baidu";
    p2="baike";
    fun(p1,p2);//交换p1和p2两个指针
    cout<<p1<<endl<<p2<<endl;//先输出baike后输出baidu
    return 0;
}

Recommended tutorial: "C# 》

The above is the detailed content of Detailed explanation of fun usage in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn