Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Functions

Functions

Scheduled Pinned Locked Moved C#
csharptutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trustapple
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • T Trustapple

      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

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • A Abhijit Jana

        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

        T Offline
        T Offline
        Trustapple
        wrote on last edited by
        #3

        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

        A A 2 Replies Last reply
        0
        • T Trustapple

          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

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • T Trustapple

            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

            A Offline
            A Offline
            Andrei Ungureanu
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • A Andrei Ungureanu

              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

              T Offline
              T Offline
              Trustapple
              wrote on last edited by
              #6

              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

              A 1 Reply Last reply
              0
              • T Trustapple

                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

                A Offline
                A Offline
                Andrei Ungureanu
                wrote on last edited by
                #7

                Jiju class is an inner class to Program in your example. Take a look at this [^] code. It is self explanatory of how inner classes work. You read also this [^]CodeProject article for more details.

                There are 10 kinds of people: those who understand binary and those who don't

                T 1 Reply Last reply
                0
                • A Andrei Ungureanu

                  Jiju class is an inner class to Program in your example. Take a look at this [^] code. It is self explanatory of how inner classes work. You read also this [^]CodeProject article for more details.

                  There are 10 kinds of people: those who understand binary and those who don't

                  T Offline
                  T Offline
                  Trustapple
                  wrote on last edited by
                  #8

                  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

                  A 1 Reply Last reply
                  0
                  • T Trustapple

                    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

                    A Offline
                    A Offline
                    Andrei Ungureanu
                    wrote on last edited by
                    #9

                    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 parameters

                    For 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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups