make 2 function type one without return and one with return and one without return…
- User
make 2 function type one without return and one with return and one without return function c language
- Assistant
Here are examples of two functions in C, one without a return type and one with a return type:
- Function without a return type (void):
c#include <stdio.h> void printMessage() { printf("Hello, World!\n"); } int main() { printMessage(); // Function call return 0; }- Function with a return type (int):
c#include <stdio.h> int addNumbers(int a, int b) { int sum = a + b; return sum; } int main() { int num1 = 5, num2 = 7; int result = addNumbers(num1, num2); // Function call printf("Sum: %d\n", result); return 0; }In the first example, the function
printMessagedoes not return any value (void), it simply prints a message to the console. In the second example, the functionaddNumberstakes two integers as input, adds them, and returns the sum as an integer. - User
For swapping two numbers
- Assistant
To swap two numbers in C, you can write a function like this:
c#include <stdio.h> void swapNumbers(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int num1 = 10, num2 = 20; printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2); swapNumbers(&num1, &num2); // Function call printf("After swapping: num1 = %d, num2 = %d\n", num1, num2); return 0; }In this example, the
swapNumbersfunction takes two integer pointersaandbas arguments. Inside the function, it uses a temporary variabletempto swap the values of*aand*b.In the
mainfunction, two numbersnum1andnum2are initially set to 10 and 20 respectively. TheswapNumbersfunction is called with the addresses ofnum1andnum2using the&operator. After the swap, the updated values are printed to the console.