C Language Learning 4--Functions


Place palindromes in an array.

Definition and use of functions

Define your own function

//Determine if i is a prime number 
int isprime(int i){
	int ret=1;
	int k;
	for(k=2;k<i;k++){
		if(i%k==0){
			ret=0;
			break;
		}
	}
	return ret;
}

call

if(isprime(i)){

}
#include<stdio.h>
//Summation 
void sum(int begin,int end) {
	int i;
	int sum=0;
	for(i=begin; i<=end; i++) {
		sum=sum+i;
	}
	printf("%d reach%d And Yes:%d\n",begin,end,sum);
}

int main() {
	sum(1,10); //Called, no return value, just function executed
	sum(20,30);
	sum(35,45);
}


A function is a block of code that takes zero or more arguments, does one thing, and returns zero or one value.

Call function:

Even without parameters, you need () when calling.

Return value from function:
If your function returns a result, you need a return.
return does two things:
1. Once a return is encountered, the function stops and no longer executes down.
2. If there is a value after return, a value is returned.

#include<stdio.h>
//Maximum 
int max(int a,int b) {   //The return type is int 
	int ret;
	if(a>b){
		ret=a;
	}
	else{
		ret=b;
	}
	return ret;   //The value returned is ret 
}

int main() {
	int a,b,c;
	a=5;
	b=6;
	c=max(a,b);   //The returned result is assigned to c 
	c=max(10,12);
	printf("%d",max(a,b));
}

Multiple return statements can appear in a function.
For example, you can modify the max function just now:

//Maximum 
int max(int a,int b) {   //The return type is int 
//	int ret;
	if(a>b){
		return a; 
	}
	else{
		return b;
	} 
}

However, this is not good and does not conform to the single export principle.

The value returned from a function can be assigned to a variable or passed back to the function. You can even throw it away.

max(12,13); No variables were assigned; There are no problems compiling. (equivalent to throwing it away)
Sometimes we call a function not to see what it returns, but to see its side effects. During the execution of this function, it may do something else, such as what it outputs.
Make a function with no return value

void Function name (parameter table){

Valued cannot be used at this time return;
Of course, you can't return;
You cannot assign a return value when calling
}

Of course, if a function has a return value, you must use return with a value.

Parameters and variables of functions

When placing functions you write behind the main() function, declare them first.

#include<stdio.h>
//Maximum 
int max(int a,int b);  //statement 

int main() {
	int a,b,c;
	a=5;
	b=6;
	c=max(a,b);   //The returned result is assigned to c 
	c=max(10,12);
	printf("%d",max(21 ,45));
}

//Write your own function after the main function, add a declaration before it can be compiled through 
int max(int a,int b) {   
//	int ret;
	if(a>b){
		return a; 
	}
	else{
		return b;
	} 
}

A semicolon at the beginning of a function is a prototype declaration.
The name of a parameter may not be written in the prototype declaration of a function, for example, as follows:

void sum(int,int);  //Declaration of function

Parameter Transfer

If a function has parameters, it must be called with the correct number and type of values passed to it.


When the value given does not match the type of the parameter, the compiler will quietly convert the type for you. But this may output unwanted results.

What was passed on in the past?

When calling a function, C can always pass only values to the function.
Each function has its own variable space, and the parameters are in this separate space, irrespective of other functions.

a is 5, b is 6, and after passing values to x and y, the value of x is 5, and the value of Y is 6.
After swapping, x becomes 6, y becomes 5. However, the values of a and b were not changed.
In the swap function, a and b do not exist.
In the main function, x and y also do not exist.
Each variable is only in the same space as his own function.
pass by value

Local variable

Each run of a function produces a separate variable space in which variables are unique to this run of the function, called local variables.
Variables defined within a function are local variables.
Local variables are also local variables, and the parameters written in the function's parameter table are local variables.

Lifetime and Scope
Lifetime: When does this variable begin to appear and when does it disappear.
Scope: In what range (code) can this variable be accessed (it works).
For local variables, the answer to both questions is the same: one block in curly braces


The main function and the swap function each have a separate variable space. When you leave the main function to call the swap function, you actually leave the variable space of the main function.
A and b in the swap function have nothing to do with a and b in the main function because they are not in the same variable space.

#include<stdio.h>
int main() {

	int a=5,b=6;

	if(a<b) {
		int i=10;
		//The variable defined in the if statement block, where the lifetime and scope are all in, does not exist without the if statement i
		printf("%d",i);
	}
	i++;
//	If you use this i outside this bracket, you will misstate undeclared
}
	
//	You can even define variables by pulling a pair of braces at random 
	{ 
		int j;
	}
//There is no problem with the program.
void f(void)
Place in parentheses void Is to indicate that this function does not take any arguments

Keywords: C

Added by eddiegster on Sat, 22 Jan 2022 08:07:56 +0200