What does passing a parameter mean? in C# [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-13 11:27:30

问题


Im fairly new to c# programming. What does passing a parameter mean? and can someone give me an example please?

Thank you.


回答1:


Functions in languages such as C# (and many, many, others) can take "parameters". These are things you pass in to let the function do whatever it was designed to do. Consider:

public int Add(int x, int y)
{
   return x + y;
}

int a = 5;
int b = 5;

int sum = Add(a, b);

In the above example, we are passing the variables a and b to the function Add. The function takes two parameters, x and y, and adds them together returning the result.




回答2:


In simple sense,passing a parameter means to give an input to the function. let me give you an example

below is the algorithm for adding two numbers and printing the result

void add(int a,int b)
{
    int sum
    sum=a+b
    print sum 
}

So we have defined a function. The function add contains two arguments. An argument is a 'holder' for the values ie a variable.Now it expects two parameters( input) a and b.

Hence to use this function we must call it like this

add(1,2) 

the numbers inside the paranthesis are called parameters. they are input for your function



来源:https://stackoverflow.com/questions/18781434/what-does-passing-a-parameter-mean-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!