Functions
-
can somebody explain to me in C# 1>what are the different ways to call a function in C#? 2>how to declare a function with parameters? 3>how to call a function with parameters? 4>MOST IMP:how to return a value form a function? its urgent.please explain in very simple terms thanking you in advance j j
-
can somebody explain to me in C# 1>what are the different ways to call a function in C#? 2>how to declare a function with parameters? 3>how to call a function with parameters? 4>MOST IMP:how to return a value form a function? its urgent.please explain in very simple terms thanking you in advance j j
Your Ans~:-D
Trustapple wrote:
1>what are the different ways to call a function in C#?
Just Write the function name from where you want to call with proper argumnet. suppose , Add(int a,int b) is a function you just call it by Add(5,6), if its return some value then s=Add(5,6). You can invoke a function using Deligation also.
Trustapple wrote:
2>how to declare a function with parameters?
int Add(int a,int b) void AddString(Sring s1,string s2)
Trustapple wrote:
3>how to call a function with parameters?
That i already explain
Trustapple wrote:
4>MOST IMP:how to return a value form a function?
just Simply use return statement int Add(int a,int b) { int c=a+b; return c; }
Happy Programming ----- Abhijit
-
Your Ans~:-D
Trustapple wrote:
1>what are the different ways to call a function in C#?
Just Write the function name from where you want to call with proper argumnet. suppose , Add(int a,int b) is a function you just call it by Add(5,6), if its return some value then s=Add(5,6). You can invoke a function using Deligation also.
Trustapple wrote:
2>how to declare a function with parameters?
int Add(int a,int b) void AddString(Sring s1,string s2)
Trustapple wrote:
3>how to call a function with parameters?
That i already explain
Trustapple wrote:
4>MOST IMP:how to return a value form a function?
just Simply use return statement int Add(int a,int b) { int c=a+b; return c; }
Happy Programming ----- Abhijit
Awesome....Thanks a lot. but i have a small doubt "Just Write the function name from where you want to call with proper argument. suppose , Add(int a,int b) is a function you just call it by Add(5,6), if its return some value then s=Add(5,6)." i didnt understand this :( now one more issue: i wrote a code with two classes.... even with out linking the classes i am getting the correct output???? can you look into this and guide me where i am going wrong please...... class Program { int x; int y; int result; char oper; public int Add(int x, int y) { result = x + y; return result; } public void display() { Console.WriteLine("{0}", result); } thanks again class jiju { static void Main() { Program obj = new Program(); obj.Add(10, 20); obj.display(); Console.ReadLine(); } } } j
-
Awesome....Thanks a lot. but i have a small doubt "Just Write the function name from where you want to call with proper argument. suppose , Add(int a,int b) is a function you just call it by Add(5,6), if its return some value then s=Add(5,6)." i didnt understand this :( now one more issue: i wrote a code with two classes.... even with out linking the classes i am getting the correct output???? can you look into this and guide me where i am going wrong please...... class Program { int x; int y; int result; char oper; public int Add(int x, int y) { result = x + y; return result; } public void display() { Console.WriteLine("{0}", result); } thanks again class jiju { static void Main() { Program obj = new Program(); obj.Add(10, 20); obj.display(); Console.ReadLine(); } } } j
Trustapple wrote:
Just Write the function name from where you want to call with proper argument. suppose , Add(int a,int b) is a function you just call it by Add(5,6), if its return some value then s=Add(5,6)." i didnt understand this
I just want to mean that if you have a function Add(int a ,int b) , u can call it by using Add(5,6) that is Void Add(int a,int b) { int c=a+b; Console.Writeline(c.ToString()); } Here no return , so u have to call it by Add(value1,value2) and if the defination is like this , that its return the value int Add(int a,int b) { return a+b; } Then u have to call it using result=Add(5,6); Hope now it clear to u.... :-D And ur code is correct.... what is the problem r u facing with it !!!!
Happy Programming ----- Abhijit
-
Awesome....Thanks a lot. but i have a small doubt "Just Write the function name from where you want to call with proper argument. suppose , Add(int a,int b) is a function you just call it by Add(5,6), if its return some value then s=Add(5,6)." i didnt understand this :( now one more issue: i wrote a code with two classes.... even with out linking the classes i am getting the correct output???? can you look into this and guide me where i am going wrong please...... class Program { int x; int y; int result; char oper; public int Add(int x, int y) { result = x + y; return result; } public void display() { Console.WriteLine("{0}", result); } thanks again class jiju { static void Main() { Program obj = new Program(); obj.Add(10, 20); obj.display(); Console.ReadLine(); } } } j
Your code works, but there are several things you can change here.
class Program
{
public int Add(int x, int y)
{
int result = x + y;
return result;
}public void display(int result)
{
Console.WriteLine("{0}", result);
}
}
class jiju
{
static void Main()
{
Program obj = new Program();
int result = obj.Add(10, 20);
obj.display(result);
Console.ReadLine();
}
}or
class Program
{
public int Add(int x, int y)
{
int result = x + y;
return result;
}
}class jiju
{
static void Main()
{
Program obj = new Program();
int result = obj.Add(10, 20);
Console.WriteLine("{0}",result);
Console.ReadLine();
}
}Hope it helps.
There are 10 kinds of people: those who understand binary and those who don't
-
Your code works, but there are several things you can change here.
class Program
{
public int Add(int x, int y)
{
int result = x + y;
return result;
}public void display(int result)
{
Console.WriteLine("{0}", result);
}
}
class jiju
{
static void Main()
{
Program obj = new Program();
int result = obj.Add(10, 20);
obj.display(result);
Console.ReadLine();
}
}or
class Program
{
public int Add(int x, int y)
{
int result = x + y;
return result;
}
}class jiju
{
static void Main()
{
Program obj = new Program();
int result = obj.Add(10, 20);
Console.WriteLine("{0}",result);
Console.ReadLine();
}
}Hope it helps.
There are 10 kinds of people: those who understand binary and those who don't
hi Andrei and abhijit, thanks for the help..... but what is confusing here is in my code i have two classes and i didnot link the two classess...... but still i am getting the result ie my function is in parent class.... but i am calling it from the child class..... but i didnot link the two classess so how is it working.....:)? can you guys help me here???? thanks again j
-
hi Andrei and abhijit, thanks for the help..... but what is confusing here is in my code i have two classes and i didnot link the two classess...... but still i am getting the result ie my function is in parent class.... but i am calling it from the child class..... but i didnot link the two classess so how is it working.....:)? can you guys help me here???? thanks again j
-
thankyou Andrei :) one more doubt while i saw a code regarding functional overloading like this..... class Program { int a; int b; int c; public Program() { } public Program(int x, int y) { a = x; b = y; } public Program(int x, int y, int z) { a = x; b = y; c = z; } public oops jiju(int x,int y ) { a=x; b=y; } public void oops(int x, int y, int z) { a = x; b = y; c = z; } my doubts are here: public Program(int x, int y) { a = x; b = y; } what is the purpose of a,b in this code...... i am a beginner with out null programming backing thats why i have so many questions ...sorry for the trouble thanks again jiju j
-
thankyou Andrei :) one more doubt while i saw a code regarding functional overloading like this..... class Program { int a; int b; int c; public Program() { } public Program(int x, int y) { a = x; b = y; } public Program(int x, int y, int z) { a = x; b = y; c = z; } public oops jiju(int x,int y ) { a=x; b=y; } public void oops(int x, int y, int z) { a = x; b = y; c = z; } my doubts are here: public Program(int x, int y) { a = x; b = y; } what is the purpose of a,b in this code...... i am a beginner with out null programming backing thats why i have so many questions ...sorry for the trouble thanks again jiju j
Trustapple wrote:
i am a beginner with out null programming backing thats why i have so many questions
First of all, if you are so new to programming start reading about OOP (Object Oriented Programming)
Trustapple wrote:
my doubts are here: public Program(int x, int y) { a = x; b = y; } what is the purpose of a,b in this code......
As you may have noticed, the "function" Program has it's name the same as the class...that means it's a constructor. In your example you have 3 constructors. One with no parameters
public Program()
{
...
}One with 2 parameters
public Program(int x, int y)
{
...
}And one with 3 parameters. In the last 2 constructor you store the values received as parameters into local variables. When you have 3 constructors you can create an object of Program type like this
Program obj = new Program(); //default constructor - no parameters
Program obj1 = new Program(1,2); //constructor with 2 parameters
Program obj2 = new Program(1,2,3); //constructor with 3 parametersFor more details about overloading constructors...try Google. Hope it helps.
There are 10 kinds of people: those who understand binary and those who don't