Total Page Preview: 000000001008
What are the types of function in C-Sharp in English and Hindi
Types of function in C-Sharp in English:
There are four types of function in c#:
1-No return value No argument
2-No return value with argument
3-With return value No argument
4-with return value with argument
here we can explain the one by one about type of function
1-No return value No argument:
In C#, a function can be defined without any arguments and without returning any value. Such functions are often used to perform some action or modify the state of the program without returning a result.
Here is an example of a C# function with no arguments and no return value:
This function is defined with the public access modifier, meaning it can be called from anywhere in the program. It is also defined with the void return type, indicating that it does not return any value.
When this function is called, it simply prints "Hello, world!" to the console, but it does not return anything.
Here is an example of how to call this function:
When this line of code is executed, it will call the SayHello function and print "Hello, world!" to the console. However, since the function does not return anything, this line of code will not receive a value.
2-No return value with argument:
In C#, a function can also be defined to accept one or more arguments without returning a value. Such a function can perform some operation or modify the program's state based on the argument values.
Here is an example of a C# function with an argument and no return value:
This function takes a string argument called message and simply prints it to the console. Again, the function is defined with the void return type, indicating that it does not return any value.
Here is an example of how to call this function with an argument:
When this line of code is executed, it will call the PrintMessage function with the argument "Hello, world!". The function will print "The message is: Hello, world!" to the console, but it does not return anything.
It is important to note that the data type and name of the argument specified in the function definition must match the data type and name of the argument provided when calling the function. If they do not match, a compilation error will occur.
3-With return value No argument:
In C#, a function can also be defined to return a value without accepting any arguments. Such a function can be used to perform some computation or return a constant value.
Here is an example of a C# function with a return value and no arguments:
This function is defined with the public access modifier and the int return type, indicating that it returns an integer value. When this function is called, it simply returns the integer value 7.
Here is an example of how to call this function and use its return value:
When this code is executed, the GetLuckyNumber function is called and returns the integer value 7. This value is then assigned to the luckyNumber variable, which is then printed to the console.
It is important to note that the return type specified in the function definition must match the data type of the value returned by the function. If they do not match, a compilation error will occur.
4- With return value with argument:
In C#, a function can also be defined to accept one or more arguments and return a value. Such a function can take inputs, perform some computation or manipulation, and return a result based on the input values.
Here is an example of a C# function with an argument and a return value:
This function takes two integer arguments a and b, adds them together, and returns the result as an integer value. The function is defined with the public access modifier and the int return type, indicating that it returns an integer value.
Here is an example of how to call this function with arguments and use its return value:
When this code is executed, the AddNumbers function is called with the arguments 2 and 3. The function adds these values together and returns the result, which is then assigned to the result variable. Finally, the value of result is printed to the console.
It is important to note that the data type and name of the arguments specified in the function definition must match the data type and name of the arguments provided when calling the function. If they do not match, a compilation error will occur. Additionally, the return type specified in the function definition must match the data type of the value returned by the function. If they do not match, a compilation error will occur.
Types of function in C-Sharp in Hindi:
C-Sharp mai 4 types k function hote hai
1-No return value No argument
2-No return value with argument
3-With return value No argument
4-with return value with argument
yaha hum 4 types k function k bare mai one by one dekhenge
1-No return value No argument:
C# mai koi bhi function bina kisi argument ke aur koi bhi value return kiye bhi define kiya ja sakta hai. Is tarah ke functions ko typically koi action perform karne ke liye ya program ki state ko modify karne ke liye use kiya jata hai.
Yahan ek C# function ka ek example diya gaya hai jisme koi argument nahi hai aur koi return value bhi nahi hai:
Is function ko public access modifier ke sath define kiya gaya hai, jisse ye program ke kisi bhi jagah se call kiya ja sakta hai. Ye function void return type ke sath define kiya gaya hai, jisse ye koi bhi value return nahi karta.
Is function ko call karne par, ye bas "Hello, world!" ko console par print karega, lekin isse koi bhi value return nahi milegi.
Is tarah se is function ko call karne ka ek example diya gaya hai:
Jab ye line of code execute kiya jayega, to ye SayHello function ko call karega aur "Hello, world!" ko console par print karega. Lekin, kyunki function koi bhi value return nahi karta hai, is line of code ko koi value nahi milegi.
2-No return value with argument:
C# mai ek function ko argument accept karne ke sath sath koi bhi value return nahi karna bhi define kiya ja sakta hai. Is tarah ke function ka upyog generally program ki state ko modify karne ke liye kiya jata hai.
Yahan ek C# function ka ek example diya gaya hai jisme ek argument hai aur koi bhi return value nahi hai:
Is function ko public access modifier ke sath define kiya gaya hai, jisse ye program ke kisi bhi jagah se call kiya ja sakta hai. Is function mai ek string type ka name argument accept kiya jata hai. Ye function void return type ke sath define kiya gaya hai, jisse ye koi bhi value return nahi karta.
Is function ko call karne par, ye bas "Hello, [name]!" ko console par print karega, jaha [name] uss value ke sath replace hoga jo function ke name argument mai provide kiya jayega.
Is tarah se is function ko call karne ka ek example diya gaya hai:
Jab ye line of code execute kiya jayega, to ye GreetUser function ko call karega aur "Hello, John!" ko console par print karega. Lekin, kyunki function koi bhi value return nahi karta hai, is line of code ko koi value nahi milegi.
3- With return value No argument:
C# mai ek function ko argument nahi lene ke sath sath koi value return karna bhi define kiya ja sakta hai. Is tarah ke functions ko typically koi value calculate karne ya kisi stored value ko return karne ke liye use kiya jata hai.
Yahan ek C# function ka ek example diya gaya hai jisme koi argument nahi hai aur ek integer value return kiya jata hai:
Is function ko public access modifier ke sath define kiya gaya hai, jisse ye program ke kisi bhi jagah se call kiya ja sakta hai. Ye function int return type ke sath define kiya gaya hai, jisse ye ek integer value return karta hai.
Is function mai ek Random object create kiya jata hai jisse ek random integer number generate kiya jata hai Next() method ke zariye, jo 1 se 100 ke beech ka koi random number generate karta hai. Ye random number GetRandomNumber() function ke through return kiya jata hai.
Is tarah se is function ko call karne ka ek example diya gaya hai:
Jab ye line of code execute kiya jayega, to ye GetRandomNumber function ko call karega aur uske return kiye hue random number ko randomNumber variable mai store karega. Fir, Console.WriteLine() method ke zariye "Random number: " ke saath randomNumber value console par print kiya jayega.
Is tarah se function ke dwara generate kiya gaya random number ab randomNumber variable mai store ho jayega.
4- With return value with argument:
C# mai ek function ko ek ya ek se jyada arguments ke sath define kiya ja sakta hai, aur sath hi sath koi value return bhi kiya ja sakta hai. Is tarah ke functions ko typically arguments ke dwara kisi calculation ya logic ke liye use kiya jata hai, aur return value ko kisi dusre function mai ya variable mai store kiya jata hai.
Yahan ek C# function ka ek example diya gaya hai jisme ek integer argument hai aur ek integer value return kiya jata hai:
Is function ko public access modifier ke sath define kiya gaya hai, jisse ye program ke kisi bhi jagah se call kiya ja sakta hai. Ye function int return type ke sath define kiya gaya hai, jisse ye ek integer value return karta hai.
Is function mai ek int type ka number argument accept kiya jata hai. Ye argument function ke through number variable ke roop mai use kiya ja sakta hai. Ye function return keyword ke zariye number variable ke square ko calculate karke return karta hai.
Is tarah se is function ko call karne ka ek example diya gaya hai:
Jab ye line of code execute kiya jayega, to ye Square function ko call karega aur number variable mai store kiye hue integer value ko Square function ke through pass karega. Ye function number ke square ko calculate karega aur return kiya hua value squaredNumber variable mai store ho jayega. Fir, Console.WriteLine() method ke zariye "Square of [number] is [squaredNumber]" ko console par print kiya jayega.
Is tarah se function ke dwara calculate kiye hue square ab squaredNumber variable mai store ho jayega.
Thank You
About Author
Brijesh Kumar
Database Developer
I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL
Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall
and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com