Id like to know how to break a loop of the main function with another function, I read a little of answers but none of them tells me if it is possible to break from a function outside the main loop. its supposed if you return 0 you end the program, but here it just still going and asking for the number, it prints the message, though.
no its not. return just ends the function you are currently in, by returning a value (unless the function returns void then it is just return). in main(), if you return a number (it doesnt have to be zero) then the program ends
#include <stdio.h>
void func1(); //returns void or nothing
int func2(); //returns an int (integer)
char func3(); //returns a char (character)
int main(int argc, char *argv[])
{
printf("calling func1\n");
func1(); //pass control to func1
printf("out of func1\n"); //return control to main
printf("calling func2\n");
printf("func2's value: %d\n", func2()); //pass control to func2 and print the value
printf("returned from func2\n"); //return from func2
printf("calling func3\n");
printf("func3's value: %c\n", func3()); //same idea
printf("out of func3");
return 1; //just return some random int
printf("You will never see this since we return'ed from main, thus exiting the program");
}
void func1()
{
printf("In func1\n");
return; //the value is omitted since it doesnt return a value
printf("this line will never be reached\n");
}
int func2()
{
printf("In func2\n");
return 1; //we return from the function, but it returns an int, so we return a 1
printf("You will never see this since we are out of the function\n");
}
char func3()
{
printf("In func2\n");
return'a'; //same as func2, except this time we return a char ('a')
printf("You will never see this since we are out of the function\n");
}
edit: forgot to have variables catch the return values